0

I wish to list the files of a few folders, in reverse alphabetical order, and then aggregate that data into a single file (AllDirectoriesFileInfo.txt). I've already figured out the sorting; now it is merely collating the info that is stumping me. Here is what I have so far:

ls -r /home/tony/Desktop/jpgfolder > AllDirectoriesFileInfo.txt
ls -r /home/tony/Desktop/giffolder > AllDirectoriesFileInfo.txt
ls -r /home/tony/Desktop/docxfolder > AllDirectoriesFileInfo.txt
ls -r /home/tony/Desktop/dregsfolder > AllDirectoriesFileInfo.txt

However, upon inspection of AllDirectoriesFileInfo.txt, only the information from dregsfolder is present, leading me to conclude that the previous data has been overwritten with each command.

So, I am aware that sed can add/insert text.

Initially I was leaning towards using a;

The syntax is as simple, simply designate the line after which a new line is to be inserted by using its corresponding number or a pattern.

However for me, specifying a line number is problematic. Say I specify line 50, will there be a massive desert of white space? Or say a larger file is utilised, then does the information become garbled?

I am aware that with the i command (insert) the user is able to specify the point at which new text is inserted.

My thinking, and pseudocode is as follows:

All information of folder 1 stored here
**END of folder 1 info**
All information of folder 2 stored here

And then from there, use SED to insert the next file after END of folder 1 info

Would this work? Or am I barking at the moon here?

  • BTW, sed does support appending to a file using sed '$a', but I can't figure out how to make it work in this situation. – wjandrea Nov 10 '17 at 22:09
3

You're barking at the moon. Just use the append redirector >> instead of the write redirector >:

touch AllDirectoriesFileInfo.txt
ls -r /home/tony/Desktop/jpgfolder >> AllDirectoriesFileInfo.txt
ls -r /home/tony/Desktop/giffolder >> AllDirectoriesFileInfo.txt
ls -r /home/tony/Desktop/docxfolder >> AllDirectoriesFileInfo.txt
ls -r /home/tony/Desktop/dregsfolder >> AllDirectoriesFileInfo.txt

Or forget about appending, and just give ls multiple arguments:

ls -r /home/tony/Desktop/jpgfolder /home/tony/Desktop/giffolder /home/tony/Desktop/docxfolder /home/tony/Desktop/dregsfolder >> AllDirectoriesFileInfo.txt

Simplified:

ls -r ~tony/Desktop/{jpgfolder,giffolder,docxfolder,dregsfolder} >> AllDirectoriesFileInfo.txt

Even shorter, assuming tony is your username:

ls -r ~/Desktop/{jpg,gif,docx,dregs}folder >> AllDirectoriesFileInfo.txt

Although, if you're going to be doing any processing on the data in the file, you'll want to use find instead of ls, since in general, ls gives output for humans to read, and find gives output for computers to read. Specifically in this case, find lists the file and its path together.

Here's a quick example:

$ mkdir dir1 dir2
$ touch {dir1,dir2}/file1
$ ls -r dir1 dir2 > ls_r_out.txt

Now say I want to search for "file1":

$ grep 'file1$' ls_r_out.txt
file1
file1

OK, that's not very helpful, cause it doesn't tell me where the files are. I'll try the same process with find. Now, find doesn't support sorting, so I'll have to pipe through sort:

$ find dir1 dir2 | sort -r > find_reverse_out.txt
$ grep 'file1$' find_reverse_out.txt 
dir2/file1
dir1/file1

Voila. Note that this gets more complicated if the filenames contain any special characters like newlines.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.