Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

I have created a simple script to move all the files with .sh suffix in the current working directory to a designated directory

If there was no such file, the script should output only an error message that I have entered, not the system error message: mv: cannot stat ‘*.sh’: No such file or directory

I tried adding ./shm 2> /dev/null after the mv command put that caused the script to run with no stop!

How can I mute the systems error message?

#!/bin/bash

echo "Moving all script files to script directory..."
mv *.sh $HOME/linux/scripts
#./shm 2> /dev/null
if [ $? -ne 0 ]
 then
        echo "No files with .sh suffix"
fi
share|improve this question

4 Answers 4

up vote 1 down vote accepted

The below script should be helpful, it will help you in displaying your custom error messages.

#!/bin/bash

files=$(ls *.sh 2> /dev/null)

if [ -z "$files" ]
then
    echo "No script files found. Exiting.."
    exit
fi

echo "Moving all script files to script directory..."

for file in $files
do
    if ! mv $file $HOME/linux/scripts
    then
        echo "Unable to move file $file"
    fi
done 

When the script gets executed, the list of "*.sh" files in the current working directory of the scripts gets assigned to files variable.

Next we do a sanity check to see if $files variable is empty. If its empty then it means that there are no "*.sh" files in the directory.

If the files exist, the for loop iterates over the $files variable and moves every file. If the mv command fails and error will be printed.

For more details about for loop refer this document.

share|improve this answer
    
In the for loop, what is the 'file'? And where did you define it? –  Mohamed Ahmed yesterday
    
The file variable gets defined and assigned in for file in $files –  Kannan Mohan yesterday
    
I guess your answer does the job, but I would like an explanation for the for loop, is it to handle the move of a file with the same name as file that already exist in the designated directory? –  Mohamed Ahmed yesterday
    
I have edited the answer and added more explanation. –  Kannan Mohan yesterday

I think it is wrong way to kill error message. As for me much better to use it for check.

#!/bin/bash

if ls *.sh &> /dev/null
then
  echo "Moving all script files to script directory..."
  mv -v -n *.sh $HOME/linux/scripts
else
  echo "No files with .sh suffix -- nothing to move"
fi
share|improve this answer

Add 2>/dev/null to you mv command to redirect stderr to /dev/null:

mv *.sh $HOME/linux/scripts 2>/dev/null
share|improve this answer

Regarding other solutions from Costas or Kannan: This

if ! mv $file $HOME/linux/scripts

should be replaced with

if ! mv "$file" "$HOME/linux/scripts/."

Also add this line somewhere at script start

 mkdir -p "$HOME/linux/scripts"

Otherwise when linux/scripts is missing, every file will be moved there as a file and overwriting the previous.

And if you have spaces in your file or folder names, the quotes will be needed.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.