Renaming a '.' File in Ubuntu

Ubuntu: Rename multiple files in different directory

If you run this command, it will use GNU Parallel to start a new bashshell in each of the directories in parallel, and run ls in each one in parallel independently:

parallel --dry-run -k 'cd {} && ls' ::: */

Sample Output

cd Dir01/ && ls
cd Dir02/ && ls
cd Dir78/ && ls

If you remove the --dry-run it will do it for real.

So, instead of running ls, let's now look at using the rename command in each of the directories. The following will rename all the files in a directory with sequentially increasing numbers ($N):

rename --dry-run '$_=$N' *

Sample Output

'file87' would be renamed to '1'
'file88' would be renamed to '2'
'file89' would be renamed to '3'
'fred' would be renamed to '4'

All the foregoing suggests the command you want would be:

parallel --dry-run -k 'cd {} && rename --dry-run "s/.*/{#}_\$N/" *' ::: */

You can run it as it is and it will just show you what it is going to do, without actually doing anything.

If you like the look of that, remove the first --dry-run and run it again and it will actually go into each subdirectory and do a dry-run of the rename, again without actually changing anything.

If you still like the look of the command, make a small copy of your files somewhere in a temporary directory and try removing both the --dry-run parameters ands if it lives up to your needs.

Ubuntu script to rename every file in folders and sub directories NOT matching sha1sum

You can try this for loop with a nested if loop

for f in /home/abcd/*; do
i=$((i+1));
sum=$(sha1sum "$f" | awk '{print $1}');
if [[ "$sum" != 3c72363260a32992c3ab2e3a5e9b8cf082e02eac ]]; then
mv "$f" "vid_$i.mp4";
fi;
done

Bulk renaming file names in Ubuntu/Linux with variable pattern

The following should work:

for f in *.fastq.gz; do echo mv "$f" "${f%%_*}_${f#*_*_*_}"; done

I specifically added echo before mv, so it prints what it would move. If it prints correctly remove echo and run again.

What happens here is I take head via %% and tail via # and concatenate them. See Parameter Expansion in man bash for meaning of %% and #. The solution relies on number of _ in file names being constant.

Batch renaming files as if editing a file of filenames

After digging around the Internet I finally found the tool:

Simply install renameutils and use qmv.

For example, $ qmv -f do.
The command generates a file containing the filenames under the current dir, editing and saving this file to apply bulk renaming.

I posted it here because I like it way more than using rename. All thanks go to the original answer, this great answer on AskUbuntu.!

Renaming files in a folder to sequential numbers

Try to use a loop, let, and printf for the padding:

a=1
for i in *.jpg; do
new=$(printf "%04d.jpg" "$a") #04 pad to length of 4
mv -i -- "$i" "$new"
let a=a+1
done

using the -i flag prevents automatically overwriting existing files, and using -- prevents mv from interpreting filenames with dashes as options.



Related Topics



Leave a reply



Submit