Need Bash command to list text file backwards. (Solved?)
Need Bash command to list text file backwards. (Solved?)
My app.: sfsinstaller deletes files installed to a Full-HD install.
To do this it makes a list of all the files contained in the installed SFS file.
The list is made with "find" & the dirs. are listed first, but they must be deleted last.
So... A simple fix is to read the file list backwards.
All I could find on this was the command "tac" (cat backwards), but Puppy no have.
I could loop through the old file & make a new backwards file, but it's inefficent.
Anyone know how to read a test file backwards or make a new one in 1 or 2 commands?
To do this it makes a list of all the files contained in the installed SFS file.
The list is made with "find" & the dirs. are listed first, but they must be deleted last.
So... A simple fix is to read the file list backwards.
All I could find on this was the command "tac" (cat backwards), but Puppy no have.
I could loop through the old file & make a new backwards file, but it's inefficent.
Anyone know how to read a test file backwards or make a new one in 1 or 2 commands?
Last edited by sunburnt on Sun 15 Apr 2007, 06:46, edited 1 time in total.
Re: Need Bash command to list text file backwards.
You can use the sort command with the -d -r optionssunburnt wrote:My app.: sfsinstaller deletes files installed to a Full-HD install.
To do this it makes a list of all the files contained in the installed SFS file.
The list is made with "find" & the dirs. are listed first, but they must be deleted last.
So... A simple fix is to read the file list backwards.
All I could find on this was the command "tac" (cat backwards), but Puppy no have.
I could loop through the old file & make a new backwards file, but it's inefficent.
Anyone know how to read a test file backwards or make a new one in 1 or 2 commands?
or better yet, use the find with the no dir option 1st then find with dir only second..
I did a google of "bash find command" and I would go with a 2 liner to make a files/delete list 1st then a dir/delete 2nd..
Here is a link to the info I found online for the "find" command;
http://www.linux.ie/newusers/beginners- ... e/find.php
Bob
or save this as "/usr/local/bin/pb-tac".
Make it executable:
chmod 755 /usr/local/bin/pb-tac
Then run
pb-tac /root/.xinitrc
It is much smaller than tac
Mark
Make it executable:
chmod 755 /usr/local/bin/pb-tac
Then run
pb-tac /root/.xinitrc
It is much smaller than tac
Mark
Code: Select all
#!/usr/bin/puppybasic
include "/usr/lib/wxbasicscript/basefunctions.inc"
thefilename = argvtostring()
thefile = readfiletolist(thefilename)
for i = count(thefile)-1 to 0 step -1
print thefile[i]
next
sed one liners to the rescue
copied from I can't remember where on the web
# reverse order of lines (emulates "tac")
# bug/feature in HHsed v1.5 causes blank lines to be deleted
sed '1!G;h;$!d' # method 1
sed -n '1!G;h;$p' # method 2
copied from I can't remember where on the web
# reverse order of lines (emulates "tac")
# bug/feature in HHsed v1.5 causes blank lines to be deleted
sed '1!G;h;$!d' # method 1
sed -n '1!G;h;$p' # method 2
Will
contribute: [url=http://www.puppylinux.org]community website[/url], [url=http://tinyurl.com/6c3nm6]screenshots[/url], [url=http://tinyurl.com/6j2gbz]puplets[/url], [url=http://tinyurl.com/57gykn]wiki[/url], [url=http://tinyurl.com/5dgr83]rss[/url]
contribute: [url=http://www.puppylinux.org]community website[/url], [url=http://tinyurl.com/6c3nm6]screenshots[/url], [url=http://tinyurl.com/6j2gbz]puplets[/url], [url=http://tinyurl.com/57gykn]wiki[/url], [url=http://tinyurl.com/5dgr83]rss[/url]
Yeeessss; It was early... Brain not functioning yet.
Sort... Of course (duh). But sort doesn't filter by file/dir. type.
bobn9lvu; There's already a list "SfsName.files" of dirs. & files made by "find".
So installing, test if current line is a dir., if it is then echo it to a ".dirs" file.
This will split the install-list file into 2 files, a ".dirs" one & a ".files" one.
Uninstall then deletes from the files-list first & then the dirs.-list second.
Then there's the dirs. which are duplicates of preexisting ones, they can't be
removed, & if they were empty before install then they would be...
So I think I'll have to do another test during install for preexisting dirs.,
& not add them to the dirs.-list file if they do exist.
This aspect makes simply reading the install-list backwards unworkable.
If anyone can think of anything else that I might have forgotten here...
Or think of a better way of going about this "install management" process...
This is the kind of support needed to further Puppy's development.
Many thanks to all you guys! ... Terry B.
Sort... Of course (duh). But sort doesn't filter by file/dir. type.
bobn9lvu; There's already a list "SfsName.files" of dirs. & files made by "find".
So installing, test if current line is a dir., if it is then echo it to a ".dirs" file.
This will split the install-list file into 2 files, a ".dirs" one & a ".files" one.
Uninstall then deletes from the files-list first & then the dirs.-list second.
Then there's the dirs. which are duplicates of preexisting ones, they can't be
removed, & if they were empty before install then they would be...
So I think I'll have to do another test during install for preexisting dirs.,
& not add them to the dirs.-list file if they do exist.
This aspect makes simply reading the install-list backwards unworkable.
If anyone can think of anything else that I might have forgotten here...
Or think of a better way of going about this "install management" process...
This is the kind of support needed to further Puppy's development.
Many thanks to all you guys! ... Terry B.
if you have a list of files and dirs in a file called /tmp/fnd.txt, you could delete them all something like this:
be careful that the dir / does not appear in the list
Code: Select all
#!/bin/sh
cat /tmp/fnd.txt | while read j
do
[ -d "$j" ] && rm -rf "$j"
[ -f "$j" ] && rm -f "$j"
done
Yes... That would be disasterous to a full-hd install.
Because of preexisting dirs., deleting dir. trees isn't a good idea.
Discriminating between deleting a single dir. & deleting a tree complicates.
Also the code to backup duplicate files has to test each & every file.
Dougal warned about using "cat" to feed long command lines, so I write it like:
Because of preexisting dirs., deleting dir. trees isn't a good idea.
Discriminating between deleting a single dir. & deleting a tree complicates.
Also the code to backup duplicate files has to test each & every file.
Dougal warned about using "cat" to feed long command lines, so I write it like:
Code: Select all
DIRS=`cat ${instFILES}/${sfsNAME}.dirs` # del. dirs. from HD
echo "$DIRS" |while read LINE
do
if [ $LINE != '^ */ *$' ];then rmdir $LINE;fi
done
Correct, but what part of the dir. structure was there before & what was added?
Original dirs. & files can't be deleted & SFS files can drop dirs. & files anywhere.
The only easy way I see to control this is to deal with one dir. or file at a time.
An apps. dir. like: /root/my-applications/AppDir/ could have itself & all it's
sub dirs. "pruned" because it & it's contense are all part of the app.
Original dirs. & files can't be deleted & SFS files can drop dirs. & files anywhere.
The only easy way I see to control this is to deal with one dir. or file at a time.
An apps. dir. like: /root/my-applications/AppDir/ could have itself & all it's
sub dirs. "pruned" because it & it's contense are all part of the app.
it would?Bruce B wrote:Maybe I'm missing something, if so forgive me is so. Wouldn't the following command accomplish your purpose?
find|sort -r
or
find|sort -r>fileout.txt
i thought that the dirs in this case would not be put 1st or last, but in alphabetical order dispersed amoungst the files?????
bob
Just so the files in each dir. were deleted before the dirs. it'd work.
But as I stated, there's a problem with this, preexisting empty dirs.
This code structure would delete them without tracking preexisting dirs.
At this point I opted to do the test while writing the installed files list.
Once the test split the dirs. & files I just decided to make both dirs. & files lists.
So the new code deletes the files list first & the dirs. list second.
But as I stated, there's a problem with this, preexisting empty dirs.
This code structure would delete them without tracking preexisting dirs.
At this point I opted to do the test while writing the installed files list.
Once the test split the dirs. & files I just decided to make both dirs. & files lists.
So the new code deletes the files list first & the dirs. list second.
i honestly don't know what you are talking about
if you have a list of files and/or dirs in a text file that you want deleted, my script will do just that ... it will delete every dir and file in the list ... it is not necessary to sort the list first, it is not necessary to delete the files first, it is not necessay to separate the list of dirs from the list of files ... my script will simply check each item in the list, and if it is a dir it will delete the dir and every file and subfolder in the dir, if it is a file it will delete the file, if it is not a valid file or dir, it will ignore it
if you want some files deleted and some not deleted, and/or you want some dirs deleted and some dirs not deleted, it can not do that (it would need to know which ones you did not want to delete)
my script simply deletes every item in the list ... nothing more, nothing less
if you have a list of files and/or dirs in a text file that you want deleted, my script will do just that ... it will delete every dir and file in the list ... it is not necessary to sort the list first, it is not necessary to delete the files first, it is not necessay to separate the list of dirs from the list of files ... my script will simply check each item in the list, and if it is a dir it will delete the dir and every file and subfolder in the dir, if it is a file it will delete the file, if it is not a valid file or dir, it will ignore it
if you want some files deleted and some not deleted, and/or you want some dirs deleted and some dirs not deleted, it can not do that (it would need to know which ones you did not want to delete)
my script simply deletes every item in the list ... nothing more, nothing less
Here's from you first postbobn9lvu wrote:it would?Bruce B wrote:Maybe I'm missing something, if so forgive me is so. Wouldn't the following command accomplish your purpose?
find|sort -r
or
find|sort -r>fileout.txt
i thought that the dirs in this case would not be put 1st or last, but in alphabetical order dispersed amoungst the files?????
bob
You're the one who said, "A simple fix is to read the file list backwards."
My app.: sfsinstaller deletes files installed to a Full-HD install.
To do this it makes a list of all the files contained in the installed SFS file.
The list is made with "find" & the dirs. are listed first, but they must be deleted last.
So... A simple fix is to read the file list backwards.
[cut]
Anyone know how to read a test file backwards or make a new one in 1 or 2 commands?
sort -r does exactly that.
Below, I've highlighted the directories in bold, they come last, by directory group. Note, in this example, the child directory and file listings come before the parent's.
./.jwm/themes/themeslist
./.jwm/themes/jwm-puppy-xp
./.jwm/themes/jwm-peach
./.jwm/themes/jwm-original
./.jwm/themes/jwm-default
./.jwm/themes/jwm-blueX
./.jwm/themes
./.jwmrc-tray
./.jwmrc-previous
./.jwmrc
./.jwm/jwmrc-personal2
./.jwm/jwmrc-personal
./.jwm
this is what i don't understand ... why do you want to delete the files first? ... my script just deletes each item in the listThe list is made with "find" & the dirs. are listed first, but they must be deleted last.
if the item is a dir, it deletes the dir and all files and subdirs, using rm -rf
if the item is a file, it deletes the file, using rm -f
(the -f in each case is not really necessary, but someone might have aliased rm to rm -i)
if the item is not a dir and is not a file, it ignores it
i honestly don't see the problem
GuestToo; The app. is the sfsInstaller for Full-HD installs of Puppy.
It mounts the SFS file & copies it's contense to / & makes an "install" file of the contense.
This is the command to make the "install" file "(sfsName).inst":
So to uninstall the app., only the files put in are deleted & only the nonpreexisting dirs.
There's also code to backup preexisting files & restore them during uninstall.
So copy is done "cp -a ....", but deleting is done one file at a time & then dirs.
So just deleting from a full dir. list would delete preexisting dirs. if they were empty.
But preexisting dirs. wouldn't be deleted if there were dirs. or files left in them.
There's code to check for preexisting dirs. & files, dirs. aren't added to the list.
Preexisting files are renamed to (FileName)_BAK-0 for backup / restore.
The Puppy Full-HD install should be exactly the way it was before the SFS was installed.
This dir. by file type of tracking's the only way I can see to assure that happens.
If preexisting dirs. aren't in the install file, & "rm -rf" isn't used, your codes good.
Bruce B; Yes the reverse list is like 2 lists, & like it, it needs filering of preexisting dir./file.
As long as the code had to ID dirs., I figured just make separate lists.
### P.S. ... If anyone wants to look at the app., I'll gladly post it for all to critique.
It mounts the SFS file & copies it's contense to / & makes an "install" file of the contense.
This is the command to make the "install" file "(sfsName).inst":
Code: Select all
find $sfsMNT |sed "s#^$sfsMNT##" > ${instFILES}/${sfsNAME}.inst
There's also code to backup preexisting files & restore them during uninstall.
So copy is done "cp -a ....", but deleting is done one file at a time & then dirs.
So just deleting from a full dir. list would delete preexisting dirs. if they were empty.
But preexisting dirs. wouldn't be deleted if there were dirs. or files left in them.
There's code to check for preexisting dirs. & files, dirs. aren't added to the list.
Preexisting files are renamed to (FileName)_BAK-0 for backup / restore.
The Puppy Full-HD install should be exactly the way it was before the SFS was installed.
This dir. by file type of tracking's the only way I can see to assure that happens.
If preexisting dirs. aren't in the install file, & "rm -rf" isn't used, your codes good.
Bruce B; Yes the reverse list is like 2 lists, & like it, it needs filering of preexisting dir./file.
As long as the code had to ID dirs., I figured just make separate lists.
### P.S. ... If anyone wants to look at the app., I'll gladly post it for all to critique.