Need Bash command to list text file backwards. (Solved?)

Using applications, configuring, problems
Post Reply
Message
Author
User avatar
sunburnt
Posts: 5090
Joined: Wed 08 Jun 2005, 23:11
Location: Arizona, U.S.A.

Need Bash command to list text file backwards. (Solved?)

#1 Post by sunburnt »

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?
Last edited by sunburnt on Sun 15 Apr 2007, 06:46, edited 1 time in total.

User avatar
bobn9lvu
Posts: 173
Joined: Wed 12 Jul 2006, 03:57

Re: Need Bash command to list text file backwards.

#2 Post by bobn9lvu »

sunburnt 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?
You can use the sort command with the -d -r options
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 8)

User avatar
MU
Posts: 13649
Joined: Wed 24 Aug 2005, 16:52
Location: Karlsruhe, Germany
Contact:

#3 Post by MU »

no, but here is tac in case you need it (attached).
It is from Mandrake 9.2.

Mark
Attachments
tac.gz
(7.94 KiB) Downloaded 240 times
Last edited by MU on Thu 12 Apr 2007, 20:53, edited 1 time in total.

User avatar
paulh177
Posts: 975
Joined: Tue 22 Aug 2006, 20:41

#4 Post by paulh177 »

surely the simplest fix is to sort the file so that the dirs. are at the end, rather than try and read it bassackwards ?

User avatar
MU
Posts: 13649
Joined: Wed 24 Aug 2005, 16:52
Location: Karlsruhe, Germany
Contact:

#5 Post by MU »

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

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

User avatar
HairyWill
Posts: 2928
Joined: Fri 26 May 2006, 23:29
Location: Southampton, UK

#6 Post by HairyWill »

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
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]

User avatar
sunburnt
Posts: 5090
Joined: Wed 08 Jun 2005, 23:11
Location: Arizona, U.S.A.

#7 Post by sunburnt »

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.

GuestToo
Puppy Master
Posts: 4083
Joined: Wed 04 May 2005, 18:11

#8 Post by GuestToo »

if you have a list of files and dirs in a file called /tmp/fnd.txt, you could delete them all something like this:

Code: Select all

#!/bin/sh
cat /tmp/fnd.txt | while read j
do
  [ -d "$j" ] && rm -rf "$j"
  [ -f "$j" ] && rm -f "$j"
done
be careful that the dir / does not appear in the list

User avatar
sunburnt
Posts: 5090
Joined: Wed 08 Jun 2005, 23:11
Location: Arizona, U.S.A.

#9 Post by sunburnt »

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:

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

GuestToo
Puppy Master
Posts: 4083
Joined: Wed 04 May 2005, 18:11

#10 Post by GuestToo »

Because of preexisting dirs., deleting dir. trees isn't a good idea.
if you choose to delete a dir, all files and subdirs would also be deleted ... if you don't delete all files and subdirs in the dir, you can't delete that dir

User avatar
sunburnt
Posts: 5090
Joined: Wed 08 Jun 2005, 23:11
Location: Arizona, U.S.A.

#11 Post by sunburnt »

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.

Bruce B

#12 Post by Bruce B »

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

User avatar
bobn9lvu
Posts: 173
Joined: Wed 12 Jul 2006, 03:57

#13 Post by bobn9lvu »

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
it would?
i thought that the dirs in this case would not be put 1st or last, but in alphabetical order dispersed amoungst the files?????

bob :?

Sage
Posts: 5536
Joined: Tue 04 Oct 2005, 08:34
Location: GB

#14 Post by Sage »

If you pipe less [ |less ] you can scroll up and down and play tunes on any line(s) you wish?

User avatar
bobn9lvu
Posts: 173
Joined: Wed 12 Jul 2006, 03:57

#15 Post by bobn9lvu »

True, but i think that this is for an automated script... :?

Bob 8)

User avatar
sunburnt
Posts: 5090
Joined: Wed 08 Jun 2005, 23:11
Location: Arizona, U.S.A.

#16 Post by sunburnt »

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.

GuestToo
Puppy Master
Posts: 4083
Joined: Wed 04 May 2005, 18:11

#17 Post by GuestToo »

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

Bruce B

#18 Post by Bruce B »

bobn9lvu wrote:
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
it would?
i thought that the dirs in this case would not be put 1st or last, but in alphabetical order dispersed amoungst the files?????

bob :?
Here's from you first post

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?
You're the one who said, "A simple fix is to read the file list backwards."

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

GuestToo
Puppy Master
Posts: 4083
Joined: Wed 04 May 2005, 18:11

#19 Post by GuestToo »

The list is made with "find" & the dirs. are listed first, but they must be deleted last.
this is what i don't understand ... why do you want to delete the files first? ... my script just deletes each item in the list

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

User avatar
sunburnt
Posts: 5090
Joined: Wed 08 Jun 2005, 23:11
Location: Arizona, U.S.A.

#20 Post by sunburnt »

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":

Code: Select all

find $sfsMNT |sed "s#^$sfsMNT##" > ${instFILES}/${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.

Post Reply