I have some files in /home/Desktop/Code directory. How can I change the file name which is last modified to a name "test.cpp" using terminal.
Ubuntu – Rename the most recently used file
files
files
I have some files in /home/Desktop/Code directory. How can I change the file name which is last modified to a name "test.cpp" using terminal.
Best Answer
Try this:
ls -t | head -n 1 | xargs -I '{}' mv '{}' test.cpp
Explanation:
ls -t
sort the files by last modification datehead -n 1
selects the first name that previous command returned (last modified file)xargs -I '{}' mv '{}' test.cpp
this command execute the mv command replacing the '{}' with string received from standard input (in this case through the pipe) (The command would bemv last_modified_file test.cpp
)To avoid directories:
`