How to add a fixed directory into an if statement? [duplicate] - linux

This question already has answers here:
Why should there be spaces around '[' and ']' in Bash?
(5 answers)
Closed 6 years ago.
I am trying to make a simple script that would move junk files into a bin like directory.
The code i wrote is here:
echo "Which file/s you want to delete?"
read fileName
if [ -d "/home/user/.waste"]
then
#moves the file to .waste
mv $fileName /home/user/.waste
echo "File deleted."
else
#creates the directory
mkdir /home/user/.waste
mv $fileName /home/user/.waste
echo "waste bin created and file deleted."
fi
When i run the script while having the directory it keeps going to the else option and does not recognize the value i entered in the if statement.

I suspect the test is incorrect (needs a space before the closing bracket).
if [ -d "DIR" ].
#!/bin/bash
WASTE_DIR="/home/user/.waste"
echo "Which file/s you want to delete?"
read fileName
if [ ! -f "$fileName" ]
then
echo "Unable to find file "$fileName"
fi
if [ -d "$WASTE_DIR" ]
then
mv "$fileName" "$WASTE_DIR"
echo "File moved to waste"
else
mkdir -p "$WASTE_DIR"
mv "$fileName" "$WASTE_DIR"
echo "$WASTE_DIR created and file moved"
fi

Related

I am trying to write a shell script to read username from a file, but it is not working. I am posting script I am writing and output [duplicate]

This question already has answers here:
How do I set a variable to the output of a command in Bash?
(15 answers)
Closed 3 years ago.
#!/bin/bash
if [ -f "$1" ]
then
for users in 'cat $1'
do
useradd $users
done
else
echo "input is not a file"
fi
You just have to get the input for the do loop right:
#!/bin/bash
if [ -f "$1" ]
then
for user in $(cat "$1")
do
useradd "$user"
done
else
echo "input is not a file"
fi
Remarks: this works for reading out a file word-by-word and I tried to keep your structure.
For reading out files line by line this is an elegant way: https://stackoverflow.com/a/4642213/2819581

How can I modify script to print information about all files in directory [duplicate]

This question already has answers here:
How to loop over files in directory and change path and add suffix to filename
(6 answers)
Closed 3 years ago.
How can I finish the script?
Linux version 3.10.0-957.21.3.el7.x86_64 .(Red Hat 4.8.5-36)
#!/bin/bash
echo "Enter the file name"
read x
if [ -f $x ]
then
echo "This is a regular file"
else
echo "This is a directory"
fi
Need modify script which will output all files and directory in /etc/ directory and indicate which one is what (e.g.:
dir1 is a directory
fileA is a file
dir2 is a directory
2nd part of the job I did. need help with
Use a for loop instead of getting the filenames from the user.
#!/bin/bash
for file in /etc/*; do
if [ -f "$file" ]
then
echo "$file is a regular file"
elif [ -d "$file" ]
then
echo "$file is a directory"
else
echo "$file is something else"
fi
done
Don't forget to quote variables, in case the value contains a space. And there are other possibilities than just files and directories.

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

Md5 Hash to identify and archive images

This is my first ever bash script and I am trying to iron out all of the creases and make the script run nicely. The script is to archive all of the specified .jpg files that it finds in multiple directories on a HDD/Flash drive. There are files with the same name but different content so I have used an Md5 sum to hash them.
I am getting the directory does not exist error in Geany but it runs fine from command bar missing out two of the images. I have tried everything I can think of to fix it. Is it messy code that is doing this?
#!/bin/sh
if [ ! -d "$1" ]; then
echo Directory "$1" cannot be found. Please try again.
exit
fi
if [ $# -eq 1 ]; then
echo "usage: Phar image_path archive_path"
exit
fi
if [ -d "$2" ]; then
echo "archive exists"
else
echo "the directory 'archive' does't exist. Creating directory 'archive'."
mkdir -p ~/archive
fi
find $1 -iname "IMG_[0-9][0-9][0-9][0-9].JPG" | cat > list.txt
[ -f ~/my-documents/md5.txt ] && rm md5.txt || break
while read line;
do md5sum $line | xargs >> md5.txt
done < list.txt
sort -k 1,1 -u md5.txt | cat > uniquemd5.txt
cut -d " " -f 2- uniquemd5.txt > uniquelist.txt
sort uniquelist.txt -r -o uniquelist.txt
for line in $(cat uniquelist.txt)
do
file=$(basename $line) path="$2/file"
if [ ! -f $path ];
then
cp $line $2
else
cp $line $path.JPG
fi
done
You haven't guarded against spaces in the folder and file names everywhere.
For instance:
cp $line $2
should be:
cp "$line" "$2"
You should start by eliminating these spaces as a source to your error by evaluating each variable you are referencing and adding ""'s.
If you still get the error please provide us with the arguments used and which directory that does not exist.

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