Morning UNIX bods.
 

  You don't need to be an 'investor' to invest in Singletrack: 6 days left: 95% of target - Find out more

Morning UNIX bods.

28 Posts
11 Users
3 Reactions
148 Views
 Earl
Posts: 1902
Free Member
Topic starter
 

I'm running this:
ls -t /mnt/subfolder/Pending/*.txt > /mnt/subfolder/Pending/FolderList.tmp

I get this:
/mnt/subfolder/Pending/file1.txt
/mnt/subfolder/Pending/file2.txt

I want this:
file1.txt
file2.txt

I can find the option in the man pages. Help please.


 
Posted : 20/03/2023 9:47 am
Posts: 13594
Free Member
 

add "| cut -d '/' -f 5" after the ls and before the ">"


 
Posted : 20/03/2023 9:50 am
Posts: 8771
Full Member
 

-1 I think

Ie

ls -1t

Oh gotyou. Not that.


 
Posted : 20/03/2023 9:50 am
Posts: 8771
Full Member
 

Easy option for loop and basename.


 
Posted : 20/03/2023 9:59 am
 Earl
Posts: 1902
Free Member
Topic starter
 

@ff

ls -t /mnt/subfolder/Pending/*.txt | cut -d ‘/’ -f 5 > /mnt/subfolder/Pending/FolderList.tmp

created a empty file.
Have I got that "| cut -d ‘/’ -f 5" in the right place?


 
Posted : 20/03/2023 10:06 am
Posts: 197
Full Member
 

ls -t /mnt/subfolder/Pending/*.txt | xargs basename -a > /mnt/subfolder/Pending/FolderList.tmp


 
Posted : 20/03/2023 10:15 am
 Earl
Posts: 1902
Free Member
Topic starter
 

Wow thanks folks.

@FF
ls -t /mnt/subfolder/Pending/*.txt | cut -d/ -f 5 > /mnt/subfolder/Pending/FolderList.tmp

gives me the fifth section - using / as the delimiter.

@dhague
| xargs basename -a
I can see how it works but no way to dream that up myself.

Thanks again


 
Posted : 20/03/2023 10:27 am
Posts: 1070
Full Member
 

find is always my go to:

find /mnt/subfolder/Pending -name \*.txt -exec basename {} \; > /mnt/subfolder/Pending/FolderList.tmp


 
Posted : 20/03/2023 10:30 am
oldnpastit reacted
Posts: 13594
Free Member
 

I can see how it works but no way to dream that up myself.

That's the interesting thing about Unix / Linux, for any command line problem there are 100s of solutions, it just depends which command syntaxes you are familiar with.....

Personally, I can never remember the syntax for 'find' and always find it counter intuitive, so just end up using ls and cut or sed to get what I want - which isn't as elegant, but works.


 
Posted : 20/03/2023 10:39 am
Posts: 77347
Free Member
 

Use Windows, it's dir /b 😁


 
Posted : 20/03/2023 11:16 am
 Earl
Posts: 1902
Free Member
Topic starter
 

Ha! I know.

I could raised a support ticket requesting they change their servers but it will probably get rejected.


 
Posted : 20/03/2023 11:24 am
Posts: 77347
Free Member
 

😁

I wasn't being serious, of course. But I've always wanted to say that as the reverse crops up on Windows threads with tedious predictability. There's some handy tips here, I've not come across xargs before.


 
Posted : 20/03/2023 11:43 am
Posts: 8771
Full Member
 

I've known of xargs for years but always found some other solution. Does it handle spaces in filenames? Ie ls -1 | xargs printf "poo %s\n”. Or I could google...


 
Posted : 20/03/2023 12:00 pm
Posts: 1226
Full Member
 

IIRC xargs will be flummoxed by spaces in filenames.


 
Posted : 20/03/2023 12:18 pm
Posts: 77347
Free Member
 

From my reading half an hour ago, it seems the -0 switch tells it not to use space as a delimiter.


 
Posted : 20/03/2023 12:24 pm
Posts: 1226
Full Member
 

This is true, but it relies on the thing generating the input playing ball, so e.g. find with the -print0 option.

If you can guarantee your files won't have spaces in the names you've got a lot more leeway, otherwise nul-delimiting is a lot more robust IME.


 
Posted : 20/03/2023 12:28 pm
Posts: 197
Full Member
 

Once more for the pedants 😜 - this version will handle spaces in filenames:

ls -t /mnt/subfolder/Pending/*.txt | xargs -L 1 -I '{}' basename "{}" > /mnt/subfolder/Pending/FolderList.tmp

"-L 1" says to pass just one argument at a time, instead of passing all of them at once (so no need for the "-a" flag to basename)
"-I {}" says to use {} as a placeholder for the argument in the following command
Putting quotes around {} in basename will allow it to handle spaces etc in the filename


 
Posted : 20/03/2023 12:46 pm
Posts: 8771
Full Member
 

Looks complicated 😃 so here is the aforementioned for loop...

d="/mnt/subfolder/Pending"; tmp="$d/FolderList.tmp"; rm tmp -f; for f in "$d/"*.txt; do basename "$f" >> "$tmp"; done


 
Posted : 20/03/2023 1:08 pm
Posts: 959
Full Member
 

I probably would have just changed directory into the target location in this case.


 
Posted : 20/03/2023 1:11 pm
Posts: 246
Free Member
 

ls -t /mnt/subfolder/Pending/*.txt | sed -e 's/.*\///' > /mnt/subfolder/Pending/FolderList.tmp


 
Posted : 20/03/2023 1:11 pm
Posts: 77347
Free Member
 

I love this.

Linux:
d=”/mnt/subfolder/Pending”; tmp=”$d/FolderList.tmp”; rm tmp -f; for f in “$d/”*.txt; do basename “$f” >> “$tmp”; done

Windows:
dir /b > FolderList.tmp

😁😁


 
Posted : 20/03/2023 1:54 pm
Posts: 7076
Full Member
 

The problem you've got is the "*.txt" is being expanded by the shell, not by ls itself, which is what would happen if you gave it a directory.

The solution above with find will avoid this.

If you really don't mind the shell globbing perhaps this would do what you want:

(cd /mnt/subfolder/Pending && ls -t *.txt > FolderList.tmp)

(No idea what's going on with the formatting in this editor).


 
Posted : 20/03/2023 2:02 pm
Posts: 7076
Full Member
 

The semicolons in some of the examples above will swallow any errors - might be an idea to replace with &&.


 
Posted : 20/03/2023 2:29 pm
Posts: 77347
Free Member
 

Aaaaand ls -t is the correct answer.

Linux nerds, man. Two dozen posts containing paragraphs of code when four characters would suffice. I swear, it's like command-line Strava to see who can come up with the most obtuse solution to any given question.

😁


 
Posted : 20/03/2023 2:34 pm
Posts: 8771
Full Member
 

Difference arises in output when using absolute path to relative path, so ls -t isn't the answer without a prior cd.

Writing paragraphs of code brightens my day 😉


 
Posted : 20/03/2023 2:41 pm
oldnpastit reacted
Posts: 7076
Full Member
 

@cougar ls -t will also list the output file, FolderList.tmp!

That's the wrongest of all the answers!

(EDIT: if the output file could go somewhere else this wouldn't be a problem but that needs an ECO for the requirements change and then we would need SVP approval).


 
Posted : 20/03/2023 2:44 pm
Posts: 77347
Free Member
 

It won't if you provide an absolute path to the output file.


 
Posted : 20/03/2023 2:45 pm
Posts: 1886
Free Member
 

[code]ls -t /your/path/here/ |grep '\.txt$' > outputfile.list[/code]


 
Posted : 20/03/2023 4:42 pm
Cougar reacted
Posts: 8771
Full Member
 

Sorry I'm sure nobody cares... BUT!

ls -t will also list the output file, FolderList.tmp!

Uh!?!? Glob for *.txt won't capture *.tmp. edit:oh okay see how you construed things.

It won’t if you provide an absolute path to the output file.

Uh!?!? Just uh!! edit: yeah uh, uh what?


 
Posted : 20/03/2023 6:54 pm

6 DAYS LEFT
We are currently at 95% of our target!