batch rename files and folders at once - bash

I got help regarding the following question:
batch rename files with ids intact
It's a great example of how to rename specific files in a group, but I am wondering if there is a similar script I could use to do the following:
I have a group of nested folders and files within a root directory that contain [myprefix_foldername] and [myprefix_filename.ext]
I would like to rename all of the folders and files to [foldername] and [filename.ext]
Can I use a similar methodology to what is found in the post above?
Thanks!
jml

Yes, quite easily, with find.
find rootDir -name "myprefix_*"
This will give you a list of all files and folders in rootDir that start with myprefix_. From there, it's a short jump to a batch rename:
find rootDir -name "myprefix_*" | while read f
do
echo "Moving $f to ${f/myprefix_/}"
mv "$f" "${f/myprefix_/}"
done
EDIT: IFS added per http://www.cyberciti.biz/tips/handling-filenames-with-spaces-in-bash.html
EDIT 2: IFS removed in favor of while read.
EDIT 3: As bos points out, you may need to change while read f to while read -d $'\n' f if your version of Bash still doesn't like it.

Related

How can I delete all files in all subdirectories with a certain name?

I have been trying to use the command line to delete all files in all subdirectories with the name s_1_1102_c.jpg.
This question is similar to what I need How to remove folders with a certain name but it is removing directories and I only want to delete the files with the name s_1_1102_c.jpg.
I will need to remove this file from 260 subdirectories under the L001 directory. My directory structure is like this:
L001
C5.1
s_1_1101_a.jpg
s_1_1101_c.jpg
s_1_1101_g.jpg
s_1_1101_t.jpg
s_1_1102_a.jpg
s_1_1102_c.jpg
s_1_1102_g.jpg
s_1_1102_t.jpg
s_1_1103_a.jpg
s_1_1103_c.jpg
s_1_1103_g.jpg
s_1_1103_t.jpg
C6.1
s_1_1101_a.jpg
s_1_1101_c.jpg
s_1_1101_g.jpg
s_1_1101_t.jpg
s_1_1102_a.jpg
s_1_1102_c.jpg
s_1_1102_g.jpg
s_1_1102_t.jpg
s_1_1103_a.jpg
s_1_1103_c.jpg
s_1_1103_g.jpg
s_1_1103_t.jpg
Ultimately I need to remove several files from all subdirectories (s_1_1101_g.jpg, s_1_1101_t.jpg, s_1_1102_a.jpg, s_1_1102_c.jpg, s_1_1102_g.jpg, s_1_1102_t.jpg). So maybe there is a way to provide a list of the file names I need to delete.
How can I delete these files?
find . -name "s_1_1102_c.jpg" -exec rm -f {} \;
Note: This will find and delete the file in any subdirectory of the current one. So you could execute it in L001 or wherever else you want to do this.
for i in s_1_1101_g.jpg s_1_1101_t.jpg s_1_1102_a.jpg s_1_1102_c.jpg s_1_1102_g.jpg s_1_1102_t.jpg; do
echo rm L001/*/"$i";
done
If output looks fine, remove echo.
The final method I used to delete my files was given by #Peter - Reinstate Monica
for f in s_1_1101_t.jpg s_1_1102_a.jpg s_1_1102_c.jpg s_1_1102_g.jpg s_1_1102_t.jpg s_1_1103_a.jpg s_1_1103_c.jpg s_1_1103_g.jpg s_1_1103_t.jpg s_1_1104_a.jpg s_1_1104_c.jpg s_1_1104_g.jpg s_1_1104_t.jpg s_1_2101_g.jpg s_1_2101_t.jpg s_1_2102_a.jpg s_1_2102_c.jpg s_1_2102_g.jpg s_1_2102_t.jpg s_1_2103_a.jpg s_1_2103_c.jpg s_1_2103_g.jpg s_1_2103_t.jpg s_1_2104_g.jpg s_1_2104_t.jpg; do find /hpc/home/L001 -name $f -delete; done
I was concerned that my file list would be too long but it worked in this situation.

Add prefix of root folder to all files in directory/subdirectories

So I have a bunch of directories that are kind of like this (but with more files):
1-0
file1
file2
folder
file 3
1-1
file1
file2
folder
file 3
etc.
And as the question asks I want to prefix the 1-0, 1-1 to all the "files" in the directories and subdirectories of the root folder being the 1-0 etc. Like follows:
1-0
1-0file1
1-0file2
folder
1-0file 3
1-1
1-1file1
1-1file2
folder
1-1file 3
etc.
I've tried a few cmd/batch solutions from other questions and ReNamer but I couldn't find anything that quite did what I wanted. Pretty inexperienced with this kind of stuff so I could have missed something for sure.
ReNamer I can do it but I have to do each directory individually and I have a fair few that I am trying to rename so it's a bit impractical.
I would find this in general a quite terrible idea to do... I cannot imagine a situation where on the long term this may really has been a good idea. One of the reasons is that it just duplicates structural information of the location in the filename itself. Why ?...
On a linux shell there are certainly various ways to accomplish this. Here is a one liner:
find . -type f -name "*" | xargs -n 1 -I {} sh -c 'file_full={}; file_bare=${file_full#./}; file=${file_bare##*/} prefix=${file_bare%%/*}; dir=${file_bare%/*}; echo mv ${file_full} ./${dir}/${prefix}${file}'
Note, there is an echo in the final output. Remove the echo only if you are really happy with the solution and want this to be executed.

Bash, move and rename files to base parent directory name

I have a bunch of base-parent-folders with different names.
In each of these base-parent-folders i have a folder with same same name(result).
In each of the result folders, i have a result file (data.txt), also named the same in each base-parent-folder.
I need to move all the data.txt files to a new folder (newfolder), and rename them to the base-parent-folder name.
for name in ./*/*/data.txt; do
mv "$name" "../newfolder/$(basename -- "$(dirname -- "$name")").txt";
done
This will move the file but rename the data.txt files to result.txt, and not the unique base-parent-folder name.
Help is much appreciated :D
What i have:
data\data1\result\data.txt
data\data2\result\data.txt
data\data3\result\data.txt
data\data4\result\data.txt
data\data5\result\data.txt
What i want:
data\newfolder\data1.txt
data\newfolder\data2.txt
data\newfolder\data3.txt
data\newfolder\data4.txt
data\newfolder\data5.txt
for f in */*/data.txt;
do mv "$f" "./newfolder/${f%/*/*}.txt";
done
Worked for me. The ./ part did not work. It wanted to add a additional folder called . when moving the file.
Thanks for the help

Simple Bash Script: Change names of files to mimic directories

I have 312 directories labeled,
Ion_0001- Ion_0312.
In each directory I have a file light.out. I'd like to change the file names in each directory to, for example:
Ion_0001.out
I believe I also need to substitute the / so that my output DOESNT look this this:
Ion_0001/.out
Can any one help me out with a simple script??
This is what I've tried:
#!/bin/bash
for dir in */
do
cd $dir
for filename in *.out; do
mv $filename ${filename//$dir.out}
done
cd ..
done
Thanks!
Not a free coding service, but it's simple enough to not make it worth arguing about...
Assuming this file structure:
Ion_0001/
Ion_0001/light.out
Ion_0002/
Ion_0002/light.out
...
Run this code in a script or just at the command line:
for i in Ion_0*
do
mv "${i}/light.out" "${i}/${i}.out"
done
Resulting in this structure:
Ion_0001/
Ion_0001/Ion_0001.out
Ion_0002/
Ion_0002/Ion_0002.out
...
Is that what you were looking for?
for dir in Ion*/; do
mv "${dir}light.out" "${dir}${dir%/}.out"
done
The trailing slash in the Ion*/ pattern limits the results to directories only, but the slash will be present in the variable's value.

Bash script to find file older than X days, then subsequently delete it, and any files with the same base name?

I am trying to figure out a way to search a directory for a file older than 365 days. If it finds a match, I'd like it to both delete the file and locate any other files in the directory that have the same basename, and delete those as well.
File name examples: 12345.pdf (Search for) then delete, 12345_a.pdf, 12345_xyz.pdf (delete if exist).
Thanks! I am very new to BASH scripting, so patience is appreciated ;-))
I doubt this can be done cleanly in a single pass.
Your best bet is to use -mtime or a variant to collect names and then use another find command to delete files matching those names.
UPDATE
With respect to your comment, I mean something like:
# find basenames of old files
find .... -printf '%f\n' | sort -u > oldfiles
for file in ($<oldfiles); do find . -name $file -exec rm; done

Resources