- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - HP-UX
- >
- Re: Parising the list of the files to be deleted
Categories
Company
Local Language
Forums
Discussions
Forums
- Data Protection and Retention
- Entry Storage Systems
- Legacy
- Midrange and Enterprise Storage
- Storage Networking
- HPE Nimble Storage
Discussions
Forums
Discussions
Discussions
Discussions
Forums
Discussions
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
- BladeSystem Infrastructure and Application Solutions
- Appliance Servers
- Alpha Servers
- BackOffice Products
- Internet Products
- HPE 9000 and HPE e3000 Servers
- Networking
- Netservers
- Secure OS Software for Linux
- Server Management (Insight Manager 7)
- Windows Server 2003
- Operating System - Tru64 Unix
- ProLiant Deployment and Provisioning
- Linux-Based Community / Regional
- Microsoft System Center Integration
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Community
Resources
Forums
Blogs
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-27-2005 07:58 AM
12-27-2005 07:58 AM
I want to clean up certain files in some log directory. I went to the log directory for listings of the files to be deleted.
I selected the files to be deleted. I redirected the output of $ll -lrt > tempfile. I edited this tempfile for the files to be deleted. Now this tempfile the contains the list of the files to be deleted.
I want to parse this tempfile to delete the files. Can anyone suggest the complete syntax of the command to be used rm command ?
Thanks,
Shiv
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-27-2005 08:03 AM
12-27-2005 08:03 AM
Re: Parising the list of the files to be deleted
Say the file "file_name" has entries
file1
file2
.....
you can try,
# cat file_name |while read FILE
> do
> rm $FILE
done
Hope this helps.
regds
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-27-2005 09:00 AM
12-27-2005 09:00 AM
Re: Parising the list of the files to be deleted
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-27-2005 09:04 AM
12-27-2005 09:04 AM
Re: Parising the list of the files to be deleted
You can dress it up any way you like it. This was pretty raw. you can use the -i option with the rm command.
Hope this helps.
regds
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-27-2005 09:24 AM
12-27-2005 09:24 AM
SolutionIf you simply add 'rm -i' to the read loop as for example:
#!/usr/bin/sh
while read LINE
do
rm -i ${LINE}
done < /tmp/files_to_remove
exit 0
...then you are mixing input from STDIN and you will *not* accomplish what you want.
Instead you need to separate (with different file descriptors) the input from the interactive 'rm' and the input from the file that you are reading:
#!/usr/bin/sh
exec 3< /tmp/files_to_remove
while read -u3 LINE
do
rm -i ${LINE}
done
exit 0
The second script will work. File descriptor #3 is used to read lines from the file '/tmp/files_to_remove'. Each LINE (file name) so read is offered for an interactive remove.
Regards!
...JRF...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-27-2005 10:30 AM
12-27-2005 10:30 AM
Re: Parising the list of the files to be deleted
If "files_to_remove" contains the list of the files to be removed then what is the purpose of word "LINE" in the script ?
Kindly explain.
Thanks,
Shiv
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-27-2005 10:48 AM
12-27-2005 10:48 AM
Re: Parising the list of the files to be deleted
You asked "If 'files_to_remove' contains the list of the files to be removed then what is the purpose of word 'LINE' in the script?"
Yes, I used the name 'files_to_remove' to represent the name of the file that contains a list of files to remove. For example:
# cat files_to_remove
/tmp/f1
/tmp/f2
/tmp/f3
/home/jrf/oldtuff
/home/shiv/oldstuff
The word 'LINE' was merely the name that I chose for a variable that holds the data from a 'read' operation. In this case, the 'read' fills the 'LINE' variable with whatever it reads up until a newline character.
Since I only expected one field on each "line" of the file, I only used one variable.
Have a look at the man pages for 'read(1)' and compare how these two scripts work by running them and typing various sentences. Use a CTRL_D to kill each script:
while read X
do
echo ${X}
done < /dev/tty
...versus:
while read X Y Z
do
echo ${X}
done < dev/tty
For example type sentences like:
who am i
what
does this help you understand?
Regards!
...JRF...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-27-2005 02:41 PM
12-27-2005 02:41 PM
Re: Parising the list of the files to be deleted
-Arun
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-27-2005 05:25 PM
12-27-2005 05:25 PM
Re: Parising the list of the files to be deleted
it would be nice that you you to get such
a kind of document "SHELLS:User guide"
Good Luck,
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-27-2005 07:33 PM
12-27-2005 07:33 PM
Re: Parising the list of the files to be deleted
with simple command in one line:
rm -i $(
1. $() executes teh command within the parenthesis
2. < reads the files_to_removes
3. rm -i remove the file asking if ok
HTH,
Art
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-27-2005 07:41 PM
12-27-2005 07:41 PM
Re: Parising the list of the files to be deleted
Here is another , you can use:
Example: the file name is : files_to_remove
# vi remove.sh
#!/usr/bin/ksh
######## Removing files from the list file: files_to_remove ########################
for i in `cat files_to_remove`
do
rm -i $i
echo "FILE: $i will be removed now"
done
echo "File Remove completed"
##########################################
To execute this :
# chmod +x remove.sh
# ./remove.sh
Cheers,
Raj.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-27-2005 07:44 PM
12-27-2005 07:44 PM
Re: Parising the list of the files to be deleted
# cd
# ls -lrt | awk '/^-/ { print $9; }' | xargs rm -f
-Muthu
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-27-2005 07:47 PM
12-27-2005 07:47 PM
Re: Parising the list of the files to be deleted
ll -lrt > tempfile is a main problem.
It will give directory, filenames etc to the tempfile. If you try to remove with rm command then it will delete files and as well as directories when proceeding with option -rf.
You can get only files in that directory not the sub directory then,
# cd
# ls -lrt | awk '/^-/ { print $9; }' | xargs rm -f
^- will give only files. $9 is the file name.
xargs used to get all files and do the rm -f action.
-Muthu
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-27-2005 07:53 PM
12-27-2005 07:53 PM
Re: Parising the list of the files to be deleted
-Arun
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-27-2005 08:01 PM
12-27-2005 08:01 PM
Re: Parising the list of the files to be deleted
You must use awk , to filter only the file names in your files_to_remove file list.
And so that it will only contains the names of the file not the permissions and date time stamp.
ex:
# ls -l |grep -v ^d | awk '{print $9}' > files_to_remove
[ It will eliminate the directory names and only files will be listed in the file ]
hth,
Raj.