Results 1 to 6 of 6
I am writing bash script for a fairly large Redhat network. I'm fairly new at scripting and still learning Linux. I am lost on this step and would greatly appreciate ...
- 02-27-2008 #1Just Joined!
- Join Date
- Feb 2008
- Posts
- 1
Advanced scripting question in my opinion
I am writing bash script for a fairly large Redhat network. I'm fairly new at scripting and still learning Linux. I am lost on this step and would greatly appreciate any help. I want to read about 500 -1000 files names in the /mnt directory and then find those same names in /export/home in order to delete them from /export/home. I hope that makes sense to someone.
So far I have setup to go into the subdirectory, but I don't know how the read all the file names. I would assume that the next step would be "cd" into /export/home to find the files I want to delete. How could apply all of the original file names to this directory and have it delete all names that match?
- 02-27-2008 #2Linux Engineer
- Join Date
- Nov 2004
- Location
- Ft. Polk, LA
- Posts
- 796
Your first step would be to look through the Advanced Bash Scripting Guide as it has many answers on shell scripting. You don't need to cd anywhere, a simple for loop will achieve what you describe:
Please do understand and test this before using it.Code:for file in /mnt/*; do rm /export/home/`basename $file` done
- 02-27-2008 #3Linux User
- Join Date
- Aug 2006
- Posts
- 458
@OP, generally ,post #2 is genearlly how you can do it, unless you also want to include subdirectories, which otherwise you would have to add extra code. Also, you can avoid a call to external command like basename if you have lots of files
orCode:ls -f /mnt | while read -r file do rm -f "/export/home/$file" done
Code:for file in /mnt/* do rm -f "/export/home/${file##*/}" done
- 02-28-2008 #4Linux Guru
- Join Date
- Nov 2007
- Location
- Córdoba (Spain)
- Posts
- 1,513
The cool way is to check if the file actually exist, before deleting it.
If you like the results, remove the "echo" and fire it up. If you want to omit dirs, you can use && [ ! -d "$i" ]for file in /wherever/*
do
i="/anyotherplace/${file##*\/}"
if [ -f "$i" ]
then
echo rm -f "$i"
fi
done
EDIT: Forget about the last comment. ! -d is not necesary, since -f already implies that $i is a normal file. So, dirs, symlinks and any other kind of files are automatically excluded anyway.
- 02-29-2008 #5Linux User
- Join Date
- Aug 2006
- Posts
- 458
well, if the intention is to remove the file anyway, there's no need to actually "check" whether the file exists or not. This check is made internally in the rm command. It will give you "no such file or directory" warning if the file is not found. therefore you can save some lines of code. However, that's just what i would do.
- 02-29-2008 #6Linux Guru
- Join Date
- Nov 2007
- Location
- Córdoba (Spain)
- Posts
- 1,513
An error is an error, as it's not only a matter of aesthetics.
The return code of the application would be 1 instead of 0, since there has been an error. That is a thing you should always care about when writing bash scripts.
If not, you can get *unexplainable* behaviors when checking for errors.
It also creates a bit of overhead by doing some innecesary i/o to stdout. Something you might not care about if the list of files is short, but try on a framebuffer device with +10k files and you will see what I mean. The check is easy, fast, and saves us a lot of small problems.


Reply With Quote
