How to delete a file from another directory in Ubuntu terminal
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
Related videos on Youtube
![R1S8K](https://lh5.googleusercontent.com/-rn-SqO0k5SY/AAAAAAAAAAI/AAAAAAAAAQc/uihnXrq5ZmE/photo.jpg?sz=256)
R1S8K
Updated on September 18, 2022Comments
-
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 fileTEST
file that is inDownloads
-
TRiG about 6 yearsDon't recommend
sudo
without (a) a good reason, and (b) a strong warning. There's unlikely to be a good reason to usesudo
to delete a file in the~/Downloads
directory.