Cannot stat mv, file not found [duplicate] - linux

This question already has answers here:
Tilde in path doesn't expand to home directory
(5 answers)
Closed 8 years ago.
could somebody please help me find the problem with this code? getting this error "mv: cannot stat '~Desktop/RecyclingBin/testtest' : No such file or directory. It does exist and it is in the location ~Desktop/RecyclingBin/testtest
fileName=$1
fileLocation='cat ~/Desktop/RecyclingBin/logs/$fileName
if [ -z "$1" ]
then
echo "please enter a valid filename"
else
echo "do you want to restore?"
read ans
if [ "$ans" =="y" ]
then
mv "~/Desktop/RecyclingBin/$fileName" "$fileLocation"
fi
fi

The quotes prevent expansion of ~. Put it outside the quotes:
mv ~/Desktop/RecyclingBin/"$fileName" "$fileLocation"

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.

get " [: if-then: unexpected operator ",when searching all sub-folder in a folder [duplicate]

This question already has answers here:
When to wrap quotes around a shell variable?
(5 answers)
Closed 4 years ago.
when I run this script in my Ubuntu:
for item in *
do
if [ -d $item ]
then
echo $item
fi
done
I got this output:
output
I don't understand why I get "[: if-then: unexpected operator"
Escape your variables. Read about it here.
for item in *
do
if [ -d "$item" ]
then
echo "$item"
fi
done

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

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

Check if a file exists in a linux script [duplicate]

This question already has answers here:
How do I tell if a file does not exist in Bash?
(20 answers)
Closed 4 years ago.
i made a linux script which receives as first argument a path to a directory. I don't know the path. And i want to check if "file.txt" exists at that certain path . For example :
if [ -e $1/file.txt ];then
echo HAHA
fi
if [[ -e "$1/file.txt" ]]; then
echo "It exists"
fi

Resources