How to delete a file from another directory in Ubuntu terminal

11,807

Solution 1

You just need to give the right path :)

To delete the TEST file in ~/Downloads you could use

rm ~/Downloads/TEST

that would work from anywhere in the filesystem because it is an absolute path. It is a shortened version of the full path /home/$USER/Downloads/TEST ($USER is expanded to the name of the current user) because ~ is a shortcut for the home directory, in my case /home/zanna

The relative path, from ~/Documents would be

rm ../Downloads/TEST

.. specifies the parent directory of where you are now, and Downloads is a subdirectory of that directory. But you should be extra careful when using rm with relative paths, because it is easy to make a mistake and delete something you didn't intend to!

Solution 2

Try this buddy :

rm -f /absolutepath/file1.a /absolutepath/file2.b

rm removes files, and -f forces it to (so that it wont stop, asking you if you want to delete the file). If this not in your home directory, prepend sudo.

rm -rf  /absolutepath/emptyOrNotFolder
Share:
11,807

Related videos on Youtube

R1S8K
Author by

R1S8K

Updated on September 18, 2022

Comments

  • R1S8K
    R1S8K almost 2 years

    I'm starting learning Linux Ubuntu commands, now I want to learn how to delete a file from another directory while I'm in a different directory.

    For example, now I'm in Documents and want to delete a file TEST file that is in Downloads

  • TRiG
    TRiG about 6 years
    Don't recommend sudo without (a) a good reason, and (b) a strong warning. There's unlikely to be a good reason to use sudo to delete a file in the ~/Downloads directory.