for i in *.txt
do
#Text files
echo $i
#checking for existing files
if [ -f ~/txt/$i ]
then
j=1
#Stripping .txt from the files
temp=${i%".txt"}
#appending filaname with counter "($j)"
i=$temp\($j\).txt
#move to folder /txt
mv $i ~/txt
else
mv $i ~/txt
fi
done
My loop checks a folder for an existing file, if that file name exists, the file name is appended (ex (1), (2) etc.
Once the file name has been renamed and it is held in $i I try to mv it but I'm getting:
mv: cannot stat 'list(1).txt': No such file or directory
I tried mv {$i} ~/txt, mv [$i] ~/txt etc...no luck. Any ideas?
You are overwriting the actually name of the file here:
i=$temp\($j\).txt
Instead, use a new variable for the new name. Something like this.
newname=$tmp\($j\).txt
#move to folder /txt
mv $i ~/txt/$newname
You say:
Once the file has been renamed and it is held in $i...
But that is wrong - the file has not been renamed at this point.
You manipulated some text in a variable. That does not have any effect on the filesystem until you run a command, such as through using mv.
Also, in your else statement, it is not clear to me why you are running mv on a file that does not exist (fails the -f test).
I had some code here, but from reading your program again, I'm not sure exactly what you're trying to accomplish, exactly...
Related
I'm now using Ubuntu, and increasingly using terminal.
I would like to delete files from Trash via command line.
So, I've gotta delete files from ~/.local/share/Trash/files dir.
All right, here's the question:
When I move some file to trash, it also creates a file_name.trashinfo file in ~/.local/share/Trash/info.
How could I automatically delete the corresponding .trashinfo file when I delete something in ../files?
You can use the following script to delete both files simultaneously. Save it in some file in the ~/.local/share/Trash directory, and call then bash <script.sh> <path-to-file-to-be-deleted-in-files-dir>.
A sample call to delete the file test if you named the script del.sh: bash del.sh files/test
#!/bin/bash
file=$1
if [ -e "$file" ] # check if file exists
then
rm -rf "$file" # remove file
base=$(basename "$file")
rm -rf "info/$base.trashinfo" # remove second file in info/<file>.trashinfo
echo 'files deleted!'
fi
I want to move a file to a folder based on its file extension.
example: if the file is .csv,it should move to COMPLETED folder , if the file has any extension other any .csv then it should move to REGULAR folder.
Below is my shell script and its not working. Can you let me know what is the problem with it?
#!/bin/bash
cd /apps/int/apd/$1/work
if ls /apps/int/apd/$1/work/*.csv &> /dev/null; then
mv *.csv /apps/int/apd/$1/COMPLETED
else
/apps/int/apd/$1/Regular
fi
Why do you have to check the existence of *.csv files?
#!/bin/bash
cd /apps/int/apd/$1/work
mv *.csv /apps/int/apd/$1/COMPLETED 2>/dev/null
mv * /apps/int/apd/$1/Regular
Here first .csv files are moved to COMPLETED folder. Then rest of the files are moved to Regular folder.
I am assuming you have created COMPLETED and Regular folders.
Change YOUR_PATH with your specific path and your path for /COMPLETED/ and /REGULAR/.
If I got what you wanted to explain i think your variables look like theese:
/YOUR_PATH/ = /apps/int/apd/$1/work
/COMPLETED/ = /apps/int/apd/$1/COMPLETED
/REGULAR/ = /apps/int/apd/$1/Regular
You can try this. :)
#!/bin/bash
for filename in /YOUR_PATH/*;
do
Path="$filename"
extension="${filename##*.}"
if [ "${extension}" = 'csv' ]; then
mv $Path /COMPLETED/
else
mv $Path /REGULAR/
fi
done
If you need anything pls leave a comment. :)
I am relatively new to bash scripting.
I need to create a script that will loop through a series of directories, go into subdirectories with a certain name, and then move their file contents into a common folder for all of the files.
My code so far is this:
#!/bin/bash
#used to gather usable pdb files
mkdir -p usable_pdbFiles
#loop through directories in "pdb" folder
for pdbDirectory in */
do
#go into usable_* directory
for innerDirectory in usable_*/
do
if [ -d "$innerDirectory" ] ; then
for file in *.ent
do
mv $file ../../usable_pdbFiles
done < $file
fi
done < $innerDirectory
done
exit 0
Currently I get
usable_Gather.sh: line 7: $innerDirectory: ambiguous redirect
when I try and run the script.
Any help would be appreciated!
The redirections < $innerDirectory and < $file are invalid and this is causing the problem. You don't need to use a loop for this, you can instead rely on the shell's filename expansion and use mv directly:
mkdir -p usable_pdbFiles
mv */usable_*/*.ent usable_pdbFiles
Bear in mind that this solution, and the loop based one that you are working on, will overwrite files with the same name in the destination directory.
The aim of my script:
look at all the files in a directory ($Home/Music/TEST) and its sub-directories (they are music files)
find out what music genre each file belongs to
if the genre is Heavy, then move the file to another directory ($Home/Music/Output)
This is what I have:
#!/bin/bash
cd Music/TEST
for files in *
do
if [ -f "$files" ];then
# use mminfo to get the track info
genre=`mminfo "$files"|grep genre|awk -F: '{print $2}'|sed 's/^ *//g'|sed 's/[^a-zA-Z0-9\ \-\_]//g'`
if [ $genre = Heavy ] ;then
mv "$files" "~/Music/Output/$files"
fi
fi
done
Please tell me how to write the mv command. Everything I have tried has failed. I get errors like this:
mv: cannot move ‘3rd Eye Landslide.mp3’ to ‘/Music/Output/3rd Eye Landslide.mp3’: No such file or directory
Please don't think I wrote that mminfo line - that's just copied from good old Google search. It's way beyond me.
Your second argument to mv appears to be "~/Music/Output/$files"
If the ~ is meant to signify your home directory, you should use $HOME instead, like:
mv "$files" "$HOME/Music/Output/$files"
~ does not expand to $HOME when quoted.
By the look of it the problem occurs when you move the file to its destination.Please check that /Music/Output/ exits from your current directory.Alternatively use the absolute path to make it safe. Also it's a good idea not use space in the file-name.Hope this will helps.:)
Put this command before mv command should fix your problem.
mkdir -p ~/Music/Output
I'm new in shell script and I am trying to figure out a way to write a script that copies all the files in the current directory to a directory specified from a .txt file and if there are matching names, it adds the current date in the form of FileName_YYYYMMDDmmss to the name of the file being copied to prevent overwritting.
Can someone help me out?
I saw thinking something along the lines of
#!/bin/bash
source=$pwd #I dont know wheter this actually makes sense I just want to
#say that my source directory is the one that I am in right now
destination=$1 #As I said I want to read the destination off of the .txt file
for i in $source #I just pseudo coded this part because I didn't figure it out.
do
if(file name exists)
then
copy by changing name
else
copy
fi
done
the problem is I have no idea how to check whether the name exist and copy and rename at the same time.
Thanks
How about this? I am supposing that the target directory is in the
file new_dir.txt.
#!/bin/bash
new_dir=$(cat new_dir.txt)
now=$(date +"%Y%m%d%M%S")
if [ ! -d $new_dir ]; then
echo "$new_dir doesn't exist" >&2
exit 1
fi
ls | while read ls_entry
do
if [ ! -f $ls_entry ]; then
continue
fi
if [ -f $new_dir/$ls_entry ]; then
cp $ls_entry $new_dir/$ls_entry\_$now
else
cp $ls_entry $new_dir/$ls_entry
fi
done
I guess this what you are looking for :
#!/bin/bash
dir=$(cat a.txt)
for i in $(ls -l|grep -v "^[dt]"|awk '{print $9}')
do
cp $i $dir/$i"_"$(date +%Y%m%d%H%M%S)
done
I assumed that a.txt contains only the name of the destination directory. If there are other entries, you should add some filter to the first statement(using grep or awk).
NB: I used full time stamp(YYYYMMDDHHmmss) instead of your YYYYMMDDmmss as it doesn't seem logical.