LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.

Notices


Reply
  Search this Thread
Old 07-27-2004, 03:49 AM   #1
ebasi
Member
 
Registered: Jul 2004
Distribution: Slackware 13.0
Posts: 51

Rep: Reputation: 15
less, cat, sed or what


Hey There, I need your help
i have a list of files created by ls /dir/ > list
Now I want to delete files from this list, some like boza=`less list`;rm $list But the files from list comes as one block. the variable $boza is file1 file2 file3 and i receive the message File name too long
How to tell less or other that I want to receive files from list line by line, not as one line all
Thanks
 
Old 07-27-2004, 04:20 AM   #2
Goala
Member
 
Registered: May 2004
Location: Merida (Spain)
Distribution: Debian
Posts: 89

Rep: Reputation: 15
Well, if I have understood you, you want remove ALL the files of a directory, don't you? (rm /dir/* would do the job). Options A), B) and C) would do the job too. But it's too easy, so I think you don't want that exactly. Perhaps you want to edit the file (that you got with the ls command) and remove files you don't want to be removed.

FIRST: BE AWARE AND DON'T DELETE IMPORTANT FILES. BACKUP THEM BEFORE THE TESTS.

you have several ways to do that:
a)
Code:
# This code would remove all the files of /dir directory. 
# Same and slower than rm /dir/*
for FILE in `ls /dir/`
do
    rm $FILE
done
b)
Code:
ls /dir/ > list
# edit list file and remove the lines of files you don't 
# want to be removed.
for FILE in `cat list`
do
    rm $FILE
done
c)
Code:
find /dir -exec echo rm {} > delete.sh
# Edit delete.sh script and remove all the lines that would 
# remove files you don't want to be removed.
chmod +x delete.sh
./delete.sh

I hope this can help you... or at least you got the idea.
bye.
 
Old 07-27-2004, 04:28 AM   #3
ebasi
Member
 
Registered: Jul 2004
Distribution: Slackware 13.0
Posts: 51

Original Poster
Rep: Reputation: 15
T he files in "list are sum links, generated by cp -s /dir1/* /dir2/
The sum links are in my apache dir the real files are somewhere in my hd.
I'll paste the script to understand my idea


boza=`less /tmp/links`
if
[ -L "$boza" ];
then
rm /var/www/htdocs/videos/"$boza";
ls /data/videos/ > /tmp/links
cp -s /data/videos/* /var/www/htdocs/videos/
else
exit
fi

in this script variable $boza comes as one block...

Last edited by ebasi; 07-27-2004 at 04:29 AM.
 
Old 07-27-2004, 05:56 AM   #4
Goala
Member
 
Registered: May 2004
Location: Merida (Spain)
Distribution: Debian
Posts: 89

Rep: Reputation: 15
I think I have understood you but I'm not sure at all :

Try to explain yourself in your own words. Don't explain me what your code does (I know it). Try to explain what you would like your code to do and don't speak about code.

I've understood you want to do this:
"I want to remove the symbolic links of /var/www/htdocs/videos/, but only those that are pointing to /data/video files because I have another symbolic links in that directory that are pointing to another directories and files. Then I want to create again symbolics links in /var/www/htdocs/videos/ pointing to all the /data/video files, and keep a file with these files to know later which files were copied (like symbolic links) to /var/www/htdocs/videos/, so if I remove files in /data/video I will not get broken links in /var/www/htdocs/videos/".

If this is what you want to do then this code would do the job:

Code:
#!/bin/bash
#
# variables:
FILE_LINKS=/tmp/links
DATA_VIDEOS=/data/videos

# delete all files present in the $FILE_LINKS file,
# but only if they are symbolic links:
if [ -r $FILE_LINKS ]
then
    for BOZA in `cat $FILE_LINKS`
    do
        FILE=/var/www/htdocs/videos/${BOZA}
        if [ -L $FILE ]
        then
            rm $FILE
        fi
    done
fi

# Recreate the $FILE_LINKS file with the actual
# contents of $DATA_VIDEOS and create symbolic
# links of all the stuff in /var/www/htdocs/videos/ :
cd ${DATA_VIDEOS}
ls -1 * > ${FILE_LINKS}
cp -s ${DATA_VIDEOS}/* /var/www/htdocs/videos/
cd -
exit

Last edited by Goala; 07-27-2004 at 08:50 AM.
 
Old 07-27-2004, 06:32 AM   #5
ebasi
Member
 
Registered: Jul 2004
Distribution: Slackware 13.0
Posts: 51

Original Poster
Rep: Reputation: 15
Yes, you understand me wright
The "rm" does not work.
I add "do" before "FILE=/var/www/htdocs/videos/${BOZA}" ...
The problem is when cat read the file. The output is file1file2file3file4file5 and rm try to remove "rm file1file2file3file4file5" not "rm file1" "rm file2" "rm file3"...

thanks!!!
Excuse my English, its horrible
 
Old 07-27-2004, 08:47 AM   #6
Goala
Member
 
Registered: May 2004
Location: Merida (Spain)
Distribution: Debian
Posts: 89

Rep: Reputation: 15
My english is not better .

You are right with the "do". I forgot it. I have not tested the code, so you can find some bugs. Sorry.

Try to change this line to arrange the "file1file2file3file4file5" problem:

ls * > ${FILE_LINKS}

change to:

ls -1 * > ${FILE_LINKS}


bye.
 
Old 07-27-2004, 09:59 AM   #7
ebasi
Member
 
Registered: Jul 2004
Distribution: Slackware 13.0
Posts: 51

Original Poster
Rep: Reputation: 15
There is no problem with the list, the problem is in output after that list is readed
Thanks for Help Goala, I appreciate it!
 
Old 07-28-2004, 12:19 AM   #8
ebasi
Member
 
Registered: Jul 2004
Distribution: Slackware 13.0
Posts: 51

Original Poster
Rep: Reputation: 15
I found the answer with your help Goala.
this is the script:

Code:
sh /tmp/delete.sh
rm /tmp/delete.sh
cp -s /data/videos/* /var/www/htdocs/videos/
cd /var/www/htdocs
for boza in *;
   do
     echo rm \"/var/www/htdocs/videos/"$boza"\" >> /tmp/delete.sh
   done
cd -
Thanks Again Goala !
 
Old 07-28-2004, 03:26 AM   #9
Goala
Member
 
Registered: May 2004
Location: Merida (Spain)
Distribution: Debian
Posts: 89

Rep: Reputation: 15
congratulations!
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
sed and escaping & in something like: echo $y | sed 's/&/_/g' prx Programming 7 02-03-2005 11:00 PM
Insert character into a line with sed? & variables in sed? jago25_98 Programming 5 03-11-2004 06:12 AM
My Cat unimaginative General 71 02-18-2004 01:08 PM
Cat ... Beuzekom Linux - Newbie 4 01-19-2004 05:32 PM
regarding cat gui10 Linux - Security 6 12-07-2001 08:09 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Software

All times are GMT -5. The time now is 10:53 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration