You don't need to be an 'investor' to invest in Singletrack: 6 days left: 95% of target - Find out more
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.
add "| cut -d '/' -f 5" after the ls and before the ">"
-1 I think
Ie
ls -1t
Oh gotyou. Not that.
Easy option for loop and basename.
@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?
ls -t /mnt/subfolder/Pending/*.txt | xargs basename -a > /mnt/subfolder/Pending/FolderList.tmp
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
find is always my go to:
find /mnt/subfolder/Pending -name \*.txt -exec basename {} \; > /mnt/subfolder/Pending/FolderList.tmp
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.
Use Windows, it's dir /b 😁
Ha! I know.
I could raised a support ticket requesting they change their servers but it will probably get rejected.
😁
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.
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...
IIRC xargs will be flummoxed by spaces in filenames.
From my reading half an hour ago, it seems the -0 switch tells it not to use space as a delimiter.
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.
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
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
I probably would have just changed directory into the target location in this case.
ls -t /mnt/subfolder/Pending/*.txt | sed -e 's/.*\///' > /mnt/subfolder/Pending/FolderList.tmp
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
😁😁
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).
The semicolons in some of the examples above will swallow any errors - might be an idea to replace with &&.
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.
😁
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 😉
@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).
It won't if you provide an absolute path to the output file.
[code]ls -t /your/path/here/ |grep '\.txt$' > outputfile.list[/code]
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?