I wan to pick the latest modified file name and redirect it to a file ..
ls -tr | tail -1 >file
but this is printing file ins side the filename ,
can anyone help me out
I wan to pick the latest modified file name and redirect it to a file ..
ls -tr | tail -1 >file
but this is printing file ins side the filename ,
can anyone help me out
ls -lrt | tail -1 | awk '{print $NF}' > file
ya the above command also gives the same output
$ ls -lrt | tail -1 | awk '{print $NF}' > file
$ cat file
file
Because by the time the command ls -tr is run the re-directed filename becomes the latest one and hence the same file-name gets stored in the same file. Try
echo $(ls -tr | tail -1) > newfile.txt
But if you run once again you get the file newfile.txt appended to itself. Check by replacing > with >>. If you need a permanent solution then redirect the output file to a file present in different location.
Or could execute in a sub-shell
(ls -tr|tail -1 > newfile.txt)
# Which when run once again has newfile.txt stored in itself.
echo $(ls -tr | tail -1) > newfile.txt
is the best solution for it....
ya that worked .. thanks everyone