Ubuntu – Rename the most recently used file


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


ls -t sort the files by last modification date

head -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 be mv last_modified_file test.cpp)

To avoid directories:

ls -t `find -maxdepth 1 -type f` | head -n 1 | xargs -I '{}' mv '{}' test.cpp


Related Question