Moving files in different folder changing the names - linux

I am trying to write a script to move some file in a common folder.
Basically I have n folders and in each of them there is a file called xmu.dat; I want to copy these files in a different folder changing its names.
This is the code I came up with (I have never written a script before...), but I get some errors:
echo "Folders found:"
for folder in */
do
echo "$folder"
name = ${folder//[\/]/}
cp ./"$folder"/xmu.dat ./OutputFiles/name
done

As fedorqui said, the issue with your code is the presence of whitespaces around the '='.
If you want to check if a file exists, you can use the '-f' option, as:
if [ -f "$file" ]
then
echo "$file found."
else
echo "$file not found."
fi

Related

searching for a file name in a directory and all its subdirectories not recognizing the file in the subdirectory

#!/bin/bash
if(($#!=1))
then
echo "Please enter a file name: "
read f1
else
f1=$1
fi
while [ ! -f $f1 ]
do
echo "Please enter an existing file name: "
read f1
done
for file in *
do
if [ $file == $f1 ]
then
pwd
fi
echo "Check here 1"
done
for file in */*
do
if [ $file == $f1 ]
then
pwd
fi
echo "Check here 2"
done
This is my script, and I have the file f3.txt in the directory having this script and a subdirectory in it.
And I want to check where this file is located. when entering the main directory, it does enter the if successfully when they find the file, but when entering the subdirectory, it won't enter the if even though f3.txt is in the subdirectory.
To clarify, the first for loop works perfectly fine, when entering the second for loop, it does echo check here 2 perfectly according to how many files there is in the subdirectory, but doesn't enter the if despite having a file f3.txt in this subdirectory
The immediate problem is that the wildcard expands to the directory name and the file name, and so of course it will not be equal to just the input file name.
if [ "$(basename "$file")" = "$f1" ]
strips the directory name before comparing (and also fixes quoting and syntax; the string equality comparison operator in sh is = and although Bash also allows == as a synonym, I can see no reason to prefer that)
... though properly speaking, you probably want
find . -name "$f1" -maxdepth 2 -ls
or maybe take out -maxdepth 2 if you want to traverse subdirectories of subdirectories etc.

Bash programs that checks folder and copies folders/files that doesen't exist there

I'm trying to make bash script for ubuntu that will check for files in folder Downloads and copy everything that doesen't exist in Documents to Documents. But I'm stuck at checking if file exist, can somebody help me out?
#!/bin/bash
cd /home/rene/Downloads
for file in *
do
echo $file
if [ ! -f /home/rene/Documents/$file ]; then
echo "File not found!"
fi
done

Bash script for for copying all files in a directory and appending with filenameCopy

I want to copy all files in a given directory, into the same directory, with a new name (something like: filenameCOPY).
I've tried a few different methods (globbing, cat, cp) but have yet to succeed. Here is where my code is currently at:
#!/bin/bash
if [ "$#" = "1"]
then
if test -d $1; then
for file in $1/*; do
//something
done
else
echo "$1 is not a directory"
fi
I think all you need is something like this:
for file in "$1"/*; do
cp -- "$file" "${file}COPY"
done;
right?

Bash renaming from path variable

I'm doing a "Recycle bin" type script, I have it all done however if a file already exists where I try to restore the file I want it to let the user choose a new name for the file to be restored as, I have the full path the file was deleted from in variable $F1 I just dont know how I go about changing the file name here
#!/bin/bash
TRASHCAN ="/home/alihassan/Desktop/Trashcan"
cd $TRASHCAN
F1=$( grep "$1" storage.txt )
if [ -f $F1 ]
then echo "File already exists at the restore location, please enter a new filename"
read ANS
mv "home/alihassan/Desktop/Trashcan/$1" "$F1/$ #ISSUE IS HERE
else
mv "/home/alihassan/Desktop/Trashcan/$1" "$F1"
fi
On the face of it, you might be after:
mv "$TRASHCAN/$1" $(dirname "$F1")/"$ANS"
This preserves the original path from $F1 for all except the last component, and then uses the answer in place of the last component.
Use parameter expansion to find the original directory:
read ANS
mv "home/alihassan/Desktop/Trashcan/$1" "${F1%/*}/$ANS"
As Jonathan commented about, there is nothing stopping you from "restoring" a file on top of an existing file, overwriting it. You might want to test for the existence of the restore location before performing that mv, eg:
while read ANS; do
if [ ! -f "${F1%/*}/$ANS" ]; then
mv "home/alihassan/Desktop/Trashcan/$1" "${F1%/*}/$ANS"
break
else
echo "File already exists - please choose another name"
fi
done

Moving multiple files in directory that have duplicate file names [duplicate]

This question already has answers here:
Moving multiple files in directory that might have duplicate file names
(2 answers)
Closed 9 years ago.
can anyone help me with this?
I am trying to copy images from my USB to an archive on my computer, I have decided to make a BASH script to make this job easier. I want to copy files(ie IMG_0101.JPG) and if there is already a file with that name in the archive (Which there will be as I wipe my camera everytime I use it) the file should be named IMG_0101.JPG.JPG so that I don't lose the file.
#!/bin/bash
if [ $# -eq 0 ]
then
echo "Usage: $0 image_path archive_path"
exit 999
fi
if [ -d "$1" ] #Checks if archive directory exists
then
echo Image Source directory FOUND
else
echo ERROR: Image Source directory has NOT BEEN FOUND
fi
if [ -d "$2" ]
then
echo Photo Archive FOUND
else
echo Creating directory
mkdir "$2"
fi
if [ find $1 -name "IMG_[0-9][0-9][0-9][0-9].JPG" ] #added this in to be more specific 1/4
then #2/4
for file in "$1"/*
do
dupefile= "$2"/"$file"
while [ -e "$newfile" ];
do
newfile=$newfile.JPG
done
mv "$file" "$newfile"
done
else #3/4
#do nothing
fi #4/4 took all the /4 out, but it's saying theres no such file or directory, even though I've tested it and it says there is.
unexpected token fi is the error I'm getting but the if statement needs to be in there so the specific files i need, are getting moved.
You cannot have an empty else. Take out the else keyword if you don't have an else part.
Also, you should get rid of the superfluous find. Just loop over the files you actually want instead.
for file in "$1"/IMG_[0-9][0-9][0-9][0-9].JPG
do
newfile= "$2"/"$file"
while [ -e "$newfile" ];
do
newfile=$newfile.JPG
done
mv "$file" "$newfile"
done
done
(This also addresses the dupefile vs newfile error.)

Resources