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

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

Related

Bash script to iterate contents of directory moving only the files not currently open by other process

I have people uploading files to a directory on my Ubuntu Server.
I need to move those files to the final location (another directory) only when I know these files are fully uploaded.
Here's my script so far:
#!/bin/bash
cd /var/uploaded_by_users
for filename in *; do
lsof $filename
if [ -z $? ]; then
# file has been closed, move it
else
echo "*** File is open. Skipping..."
fi
done
cd -
However it's not working as it says some files are open when that's not true. I supposed $? would have 0 if the file was closed and 1 if it wasn't but I think that's wrong.
I'm not linux expert so I'm looking to know how to implement this simple script that will run on a cron job every 1 minute.
[ -z $? ] checks if $? is of zero length or not. Since $? will never be a null string, your check will always fail and result in else part being executed.
You need to test for numeric zero, as below:
lsof "$filename" >/dev/null; lsof_status=$?
if [ "$lsof_status" -eq 0 ]; then
# file is open, skipping
else
# move it
fi
Or more simply (as Benjamin pointed out):
if lsof "$filename" >/dev/null; then
# file is open, skip
else
# move it
fi
Using negation, we can shorten the if statement (as dimo414 pointed out):
if ! lsof "$filename" >/dev/null; then
# move it
fi
You can shorten it even further, using &&:
for filename in *; do
lsof "$filename" >/dev/null && continue # skip if the file is open
# move the file
done
You may not need to worry about when the write is complete, if you are moving the file to a different location in the same file system. As long as the client is using the same file descriptor to write to the file, you can simply create a new hard link for the upload file, then remove the original link. The client's file descriptor won't be affected by one of the links being removed.
cd /var/uploaded_by_users
for f in *; do
ln "$f" /somewhere/else/"$f"
rm "$f"
done

Moving files in different folder changing the names

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

Check that two file exists in UNIX Directory

Good Morning,
I am trying to write a korn shell script to look inside a directory that contains loads of files and check that each file also exists with .orig on the end.
For example if a file inside the directory is called 'mercury_1' there must also be a file called 'mercury_1.orig'
If there isn't, it needs to move the mercury_1 file to another location. However if the .orig file exists do nothing and move onto the next file.
I am sure it is really simple but I am not that experienced in writing Linux scripts and help would be greatly appreciated!!
Here's a small ksh snippet to check if a file exists in the current directory
fname=mercury_1
if [ -f $fname ]
then
echo "file exists"
else
echo "file doesn't exit"
fi
Edit:
The updated script that does the said functionality
#/usr/bin/ksh
if [ ! $# -eq 1 ]
then
echo "provide dir"
exit
fi
dir=$1
cd $dir
#process file names not ending with orig
for fname in `ls | grep -v ".orig$"`
do
echo processing file $fname
if [ -d $fname ] #skip directory
then
continue
fi
if [ -f "$fname.orig" ] #if equiv. orig file present
then
echo "file exist"
continue
else
echo "moving"
mv $fname /tmp
fi
done
Hope its of help!
You can use the below script
script.sh :
#!/bin/sh
if [ ! $# -eq 2 ]; then
echo "error";
exit;
fi
for File in $1/*
do
Tfile=${File%%.*}
if [ ! -f $Tfile.orig ]; then
echo "$File"
mv $File $2/
fi
done
Usage:
./script.sh <search directory> <destination dir if file not present>
Here, for each file with extension stripped check if "*.orig" is present, if not then move file to different directory, else do nothing.
Extension is stripped because you don't want to repeat the same steps for *.orig files.
I tested this on OSX (basically mv should not differ to much from linux). My test directory is zbar and destination is /tmp directory
#!/bin/bash
FILES=zbar
cd $FILES
array=$(ls -p |grep -v "/") # we search for file without extension so put them in array and ignore directory
echo $array
for f in $array #loop in array and find .orig file
do
#echo $f
if [ -e "$f.orig" ]
then
echo "found $f.orig"
else
mv -f "$f" "/tmp"
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.)

Issue creating a folder then moving files into it within the same script

I'm having problems moving files into a folder after I create it in a shell script.
My script looks like:
#!/bin/bash
echo -e "Processing\033[36m" $1 "\033[0mwith the German script";
if [ ! -d ${1%.dat} ]; then
echo -e "making directory\033[33m" ${1%.dat} "\033[0msince it didn't exist...";
mkdir ${1%.dat};
fi
...processing occurs here... (irrelevant to issue)
if [ -d ${1%.dat} ]; then
mv useragents_$1 /${1%.dat}/useragents_$1;
mv summary_$1 /${1%.dat}/summary_$1;
more /${1%.dat}/useragents_$1;
else
echo -e "\033[31mERROR: cannot move files to folder.\033[0m";
fi
As you can see I create the folder if it doesn't exist in the top section and then if it exists I move the files into that folder in the bottom section, the problem is that it doesn't create the folder in time to move the files in (I'm assuming) so when it reaches the lower code, I only get the ERROR.
I tried using, sleep 5, but it only slows down the script and has no effect on the ERROR.
I would really appreciate some advice.
Errors below:
mv: cannot move `useragents_100_stns2_stns6.dat' to `/100_stns2_stns6/useragents_100_stns2_stns6.dat': No such file or directory
mv: cannot move `summary_100_stns2_stns6.dat' to `/100_stns2_stns6/summary_100_stns2_stns6.dat': No such file or directory
/100_stns2_stns6/useragents_100_stns2_stns6.dat: No such file or directory
Pass 1
Your check:
if [ ! -d ${1%.dat} ]; then
should be:
if [ -d ${1%.dat} ]; then
You created the directory; if it is a directory, move stuff into it.
Typo in question
Pass 2
You create:
mkdir ${1%.dat}
You try to move files:
mv useragents_$1 /${1%.dat}/useragents_$1;
Note the leading slash in the move compared to the create. Make those consistent.
Are you sure of this part ? It uses a root directory.
/${1%.dat}/summary_$1;
You probably want to do this instead:
${1%.dat}/summary_$1;
It allows you to move the file into the directory IN your current directory.

Resources