bash - Get index of argument with xargs? -


in bash, have list of files named same (in different sub directories) , want order them creation/modified time, this:

ls -1t /tmp/tmp-*/my-file.txt | xargs ... 

i rename files sort of index or can move them same folder. result ideally like:

my-file0.txt my-file1.txt my-file2.txt 

something that. how go doing this?

you can loop through these files , keep appending incrementing counter desired file name:

for f in /tmp/tmp-*/my-file.txt;    fname="${f##*/}"    fname="${fname%.*}"$((i++)).txt     mv "$f" "/dest/dir/$fname" done 

edit: in order sort listed files modification time case ls -1t can use script:

while ifs= read -d '' -r f;    f="${f#* }"    fname="${f##*/}"    fname="${fname%.*}"$((i++)).txt     mv "$f" "/dest/dir/$fname" done < <(find /tmp/tmp-* -name 'my-file.txt' -printf "%t@ %p\0" | sort -zk1nr) 

this handles filenames special characters white spaces, newlines, glob characters etc since ending each filename nul or \0 character in -printf option. note using sort -z handle nul terminated data.


Comments

Popular posts from this blog

account - Script error login visual studio DefaultLogin_PCore.js -

xcode - CocoaPod Storyboard error: -