I am trying to rename the files and directories using a text file separated by space.
The files.txt looks like this:
dir1-1 dir1_1
dir2-1 dir223_1
My command is as follows:
xargs -r -a **files.txt** -L1 mv
The files look like this (in directory 1):
dir1-1.txt dir1-1.gzip dir1-1-something.text
The output for the files should look like this (in the dir1-1):
dir1_1.txt dir1_1.gzip dir1_1-something.text
The files in directory (dir2) looks like this:
dir2-1.py dir2-1.txt dir2-1.text
The output should look like this:
dir223_1.py dir223_1.txt dir223_1.text
This command can rename only folders from dir1-1 to dir1_1 and dir2-1to dir223_1so on but it doesn't rename the files in the subdirectories. The files in the corresponding directories also have these prefix of these directories. (like dir1-1.txt dir1-1.gzip dir1-1.csv)
It will be somewhat similar like: Script for renaming files using text file containing alternate file names
Looking forward for the assistance.
xargs
command work only on file lines. Somv
is only called on those values. Do you put in your question an example of file names and directories/sub-directories structure and of course an example of what do you expect. Thanksrename
utility. There are many questions with detailed answers on how to use it here on this site. It can handle any kind of renaming task, from simple sed-like filename transformations (s/old/new/
) to a a complex script using any valid perl code.rename
utility. But I am unable to use it via text file. I have used multiple options to rename the file using the extensions, specific pattern in the file name, replacing specific characters, finding the names in subdirectories and moving them but none of these condition apply to my query. (using a text file with an old name and new name in the file)rename
, using perl'sopen()
function to open thefiles.txt
file for read, then iterate over lines in the file and split each line on white-space....but for a job like this, it's probably easier to just write a perl script without using the perlrename
tool. or a bash script as in my answer below.