Home > Mobile >  How to touch all files that are returned by a sorted ls?
How to touch all files that are returned by a sorted ls?

Time:01-12

If I have the following:

ls|sort -n 

How would I touch all those files in the order of the sorted files? Something like:

ls|sort -n|touch

What would be the proper syntax? Note that I need to sort touch the files in the exact order they're being sorted -- as I'm trying to sort these files for a FAT reader with minimal metadata reading.

CodePudding user response:

You can use this command

(ls|sort -n >> list.txt )    
touch $(cat list.txt)

OR

touch $(ls /path/to/dir | sort -n)

OR if you want to copy files instead of creating empty files use this command

cp list.txt ./DirectoryWhereYouWantToCopy

CodePudding user response:

Can you give a few file name?
if you have file names with numbers as 1file, 10file, 11file .. 20file, then you need use --generic-numeric-sort

ls | sort --general-numeric-sort --output=../workingDirectory/sortedFiles.txt
cat sortedFiles.txt
1file
10file
11file
12file
20file

and move sortedFile.txt into your working directory or where ever you want.

touch $(cat ../workingDirectory/sortedFiles.txt)

this will create empty files with the exact same name

  •  Tags:  
  • Related