There are multiple directories which contain a file with the same name:
direct_afaap/file.txt
direct_fgrdw/file.txt
direct_sardf/file.txt
...
Now I want to extract them to another directory, direct_new
and with a different file name such as:
[mylinux~ ]$ ls direct_new/
file_1.txt file_2.txt file_3.txt
How can I do this?
BTW, if I want to put part of the name in original directory into the file name such as:
[mylinux~ ]$ ls direct_new/
file_afaap.txt file_fgrdw.txt file_sardf.txt
What can I do?
It may not be quite what you want, but it will do the job. Use
cp --backup=numbered <source_file> <destination_directory
:If you really want to put part of the folder name in, you have to decide exactly what part you want. You could, of course, just replace the directory separator character with some other character, and put the whole path into the filename.
This little BaSH script will do it both ways:
And the output:
where file_list is a list of your files.
You can achieve this with Bash parameter expansion:
dir="${file%/*}"
removes all characters starting from/
, basically, giving us the dirname${dir#*direct_}
removes thedirect_
prefix from dirname((++counter))
uses Bash arithmetic expression to pre-increment the counterSee also: