Renaming multiples files from differernt directories using shell script - shell

I have 50 directories and inside each directory I have a file of the name hist_anh_abs_0. So I have 50 files of the
name hist_anh_abs_0
What I want is the following, I want to run a loop over all 50 directories and which will pull out the file name hist_anh_abs_0 and rename it like hist_anh_abs_0_1, hist_anh_abs_0_2... and finally put all the renamed files in a separate directory.
I did something like this
for file in hist_anh_abs_0
do
mv -i "${file}" "${file/-_abs_0-/-_abs_0_$i-}"
done
But I can't do the part where it will read all the directories and look for the file of name hist_anh_abs_0

ct=0 ;
for f in */hist_anh_abs_0
do
mv $f ${destdir}/${f}_$((ct++));
done

Related

Shell script copy file from one dir to another

I am trying to copy a file from a dir to another directory location using regx * like below
cp /location1/abc_* /location/xyz_
Its failing to copy because In location1 there are many files with abc_123 abc_234 like these.
Seems like command get confuse to copy which file as xyz_
I want to copy only the file which is recent abc_ file. Is there any way to achieve this requirement?
You can iterate over the glob to select the most recent file, then copy that one file. (Every file compares as "newer" than a non-existent file
newest=
for f in /location1/abc_*; do
[[ $f -nt $newest ]] && newest=$f
done
cp "$newest" /location/xyz_/

Unable to rename and move files from one location to another

I need to add prefix C_ and then move files from tmp location to target location.
Here is the script
I am not allowed to place script in current directory.
for tmpfile in /home/asmita/tmp
do
mv "$tmpfile" "C_${tmpfile}"
mv "C_${tmpfile}" /home/tgasmita
done
When I try moving prefixed files I get error C_/home/asmita/tmp/xyz.txt not found. as entire path is stored in tmpfile variable.
Change your code to use the basename and the dirname command to get the filename and the directory name. Use these to combine the values and get the new path.
for tmpfile in /home/asmita/tmp
do
DIRPATH=$(dirname "${tmpfile}")
FILENAME=$(basename "${tmpfile}")
mv "$tmpfile" "${DIRPATH}C_${FILENAME}"
mv "${DIRPATH}C_${FILENAME}" /home/tgasmita
done
There are many ways to do this. First let me fix your script.
1.In below code make sure that you are passing only filename not the full path. Below script is valid only if know the FILENAME
#!/bin/sh
for tmpfile in /home/asmita/tmp
do
mv "{tmpfile}/filename.txt" "/home/tgasmita/C_filename.txt"
done
2.If you do not know file name and you want to rename and move .txt files to another folder then you might like below script.
#!/bin/sh
lines=`find /home/asmita/tmp -name "*.txt" -printf "%f\n";`
for i in ${lines[#]}
do
mv "/home/asmita/tmp/${i}" "/home/tgasmita/C_${i}"
done
Please note that I m looking for only text file in source folder(/home/asmita/tmp
) You can change .txt to other extension according to requirement. If you want to move and rename all files from source folder then just replace "*.txt" to ".*" from second line.

How do I batch copy and rename files with same name in different path?

I have a set of folders, inside each folder I have several sub-folders. In each sub folder there is a file called result.txt I need all the result.txt to be copied to other location renaming result files.
folders:
abc1
efg1
doc2
ghih
sub-folders:
in aaa1
1.abc1.merged
2.abc1.merged
3.abc1.merged
4.abc1.merged
in efg1
1.efg1.merged
2.efg1.merged
3.efg1.merged
4.efg1.merged
5.efg1.merged
...
...
so on
all of the sub-folders contain result.txt in a single different folder with all the result files renamed to result1.txt,result2.txt etc.
I tried to set name of sub-folder as a variable in shell script and made a loop to go in to the sub-folder and copy the result.txt to other path and rename it by mv command.But only the result.txt file from one subdirectory each is copied not the all.
I tried with following commands:
cp $folder/$subfolder/resu*txt ../tsv/$newfolder/
(I previously assigned variables)
mv ../tsv/$newfolder/resu*txt ../tsv/$newfolder/results$tts.txt
(I defined $tts as number of subfolders in the folder)
This copied the result.txt from only first sub-folder in each of parent folders.
I would say something like:
i=1
for dir in **/*; # loop through files
do
cp "$dir"/result.txt $your_path/result$i.txt
((i++)) # increment variable $i
done
The syntax **/* goes through all the subdirectories.
Test
$ mkdir a
$ cd a
$ mkdir a{1..4}
$ mkdir a{1..4}/a{1..4}
$ for f in **/*; do echo "$f --"; done
The last command returns
a1/a1 --
a1/a2 --
...
a4/a4 --

Script to move files

I used a script to create sub-directories based on the file names of all my mp4 files. My files and newly created sub directories, of the same name, are located in the smae sub directory. Now I need a script to move the mp4 files into each of the files corresponding sub directories. I hope this makes sense. ex: I would like to move "crank (2006).mp4" to the sub directory named "crank (2006)". I have about 1200 of these files to move to their already created sub directories. Please help.
Removing the .mp4 suffix uses %% to delete the sub-string .mp4 from the end of the $f variable.
The mkdir statement ensures that the sub-directory does exist before the mv command.
for f in *.mp4
do
subdir="${f%%.mp4}"
mkdir -p "$subdir"
mv "$f" "$subdir"
done
mmv 'smae/*.mp4' 'smae/#1/#1.mp4'
This is much safer than (noddy) scripts as mmv will check for loops, name collisions, possible problems in the move before moving any file etc.
Following code will,
find the .mp4 files in current directory,
create sub-directories based on the file names of all mp4 files,
move each of the files to corresponding sub directories
for f in *.mp4; do path=$(ls $f | rev | cut -c 5- | rev); mkdir $path; mv $f $path/. ; done
Ex: if "crank (2006).mp4" is available into current directory than new sub directory named "crank (2006)" will be created into current directory and "crank (2006).mp4" file will be moved into that sub-directory.
NOTE: instead of "mv" you can also use "cp" for copy files

script for auto opening directories (mac os x) and creating m3u playlists

Imagine that you have some directories containing both mp3 files and sub-directories, also containing mp3 files, like:
/music/band1/
/music/band2/
/music/band2/dir1/
/music/band2/dir2/
/music/band3/dir1/
/music/band3/dir2/
/music/band4/
...
I would like to create a .m3u file in every directory containing mp3 files - so for example:
/music/band1/band1.m3u
/music/band2/band2.m3u
/music/band2/dir1/dir1.m3u
/music/band2/dir2/dir2.m3u
/music/band3/dir1/dir1.m3u
/music/band3/dir2/dir2.m3u
/music/band4/band4.m3u
The name of .m3u file will be the directory name in which the .m3u is created. (This is just an example directory structure.)
I hope it is clear :)
So far I have this to generate m3u files from *.mp3 files, but only in the actual directory, and name it by directory.
#!/bin/bash\
ls | grep -i mp3 > filelist.txt
mv filelist.txt filelist.m3u
FOLDERNAME=${PWD##*/}
echo $FOLDERNAME
mv filelist.m3u $FOLDERNAME.m3u
What should be added to have it look recursively into every subdirectory of /music, and if there is another sub-sub directory, also look there? Then, execute the m3u-making script in each sub-directory?
Thank you for your help! :)
This should take care of the whole thing for you (no need for the script).
find . -name '*.mp3' -execdir bash -c 'file="{}"; printf "%s\n" "${file##*/}" >> "${PWD##*/}.m3u"' \;

Resources