How to delete files and directories in the Linux terminal

the rm and rmdir Commands delete files and directories on Linux, macOS, and other Unix-like operating systems. They are similar to that del and deltree Commands in Windows and DOS. These commands are very powerful and have a few options.

It is important to note that files and directories are deleted with rm and rmdir do not move to the trash. They will be removed from your computer immediately. If you accidentally delete files using these commands, the only way to restore them is from a backup.

How to remove files with rm

The simplest case is to delete a single file in the current directory. Write the rm Command, a space, and then the name of the file you want to delete.

rm file_1.txt

If the file is not in the current working directory, provide a path to where the file is saved.

rm ./path/to/the/file/file_1.txt

You can have more than one filename. to hand over rm. This will delete all specified files.

rm file_2.txt file_3.txt


Wildcards can be used to select groups of files to delete. the * represents multiple characters and the ? stands for a single character. This command would delete all PNG image files in the current working directory.

rm *.png

This command would delete all files that have a single character extension. To the example, this would delete File.1 and File.2, but not File.12.

rm *.?

If a file is read-only, you will be prompted before deleting the file. You have to answer with y or n and press “Enter. “

To reduce the risk of using it rm with wildcards use the -i (interactive) option. To do this, you have to confirm the deletion of each file.

rm -i *.dat

rm command in interactive mode

the -f The (force) option is the opposite of interactive. It doesn’t ask for confirmation even if files are read-only.

rm -f filename

How to remove directories with rm

To remove an empty directory, use the -d (Directory) option. You can use placeholders (* and ?) in directory names as well as in file names.

rm -d directory


If you specify more than one directory name, all specified empty directories are deleted.

rm -d directory1 directory2 /path/to/directory3

To delete non-empty directories, use the -r (recursive) option. For the sake of clarity, this removes the directories and all files and subdirectories they contain.

rm -r directory1 directory2 directory3

If a directory or file is write-protected, you will be asked to confirm the deletion. To delete non-empty directories and suppress these prompts, use the -r (recursive) and -f (force) options together.

rm -rf directory

Care is required here. Make a mistake with that rm -rf Command can result in data loss or system errors. It is dangerous and caution is the best policy. To understand the directory structure and files that the rm -rf Command, use the tree Command.

Use apt-get to install this package on your system if you are using Ubuntu or any other Debian based distribution. For other Linux distributions, use your Linux distribution’s package management tool instead.

sudo apt-get install tree

Executive tree The command creates an easy-to-understand diagram of the directory structure and the files under the directory from which it is executed.

tree

Output from the tree command


You can also choose a path to the tree Command to start the tree from another directory in the file system.

tree path/to/directory

the rm Command also has --one-file-system, --no-preserve-root, --preserve-root Options, however, only recommended for experienced users. If you do something wrong, you can accidentally delete all of your system files. Consult the command lines Manual page for more informations.

How to remove directories with rmdir

There is another command called rmdir, that you can use to delete directories. The difference between rm and rmdir is that rmdir can only delete empty directories. It will never delete files.

The simplest case is to delete a single empty directory. As in rm, you can use multiple directory names. to hand over rmdir , or a path to a directory.

Delete a single directory in the current directory by adding its name. to hand over rmdir :

rmdir directory

Delete multiple directories by making a list of names. to hand over rmdir :

rmdir directory1 directory2 directory3


Delete a directory that is not in the current directory by specifying the full path to that directory:

rmdir /path/to/directory

If you try to delete a folder that is not empty, rmdir gives you an error message. Hereinafter example rmdir successfully and silently deletes the clients Directory, but it refuses to delete that projects Directory because it contains files. the projects The directory is left unchanged and the files it contains remain unaffected.

rmdir command with a non-empty folder

When rmdir If you get a “Directory not empty” error message, it will stop processing the directories given to it on the command line. When prompted to delete four directories and the first directory contains files, rmdir would give you the error message and do nothing more. You can force it to ignore these errors with the --ignore-fail-on-non-empty Option to process other directories.

Hereinafter example two folders were passed to rmdir, these are work/reports and work/quotes . the --ignore-fail-on-non-empty Option added to command. the work/reports Folder contains files, so rmdir can’t delete it. the --ignore-fail-on-non-empty Option forces rmdir to ignore the error and move to the next folder that needs to be processed, namely work/quotes. This is an empty folder, and rmdir clears it.

This was the command used.

rmdir --ignore-fail-on-non-empty work/reports /work/quotes

rmdir with --ignore-fail-on-non-empty option

You can use the … -p (Parents) Option to delete a directory and delete its parent directories. This trick works because rmdir starts with the target directory and then goes back to the parent directory. This directory should now be empty so that it can be deleted by rmdirand it repeats to reset the path that was provisioned rmdir.

Hereinafter example the command given to. is passed rmdir is:

rmdir -p work/invoices

Rmdir command with option to remove parents


Both invoices and the work Directories are deleted as required.

Whether you’re using bash or another shell, Linux has flexible and powerful commands that let you delete directories and files right from the terminal command line. Some people prefer a workflow that revolves around the terminal. Others may not have a choice on this matter. You can work on servers with no GUI installed or in a remote session on a headless system such as a Raspberry Pi. These commands are perfect for this group of people.

But whatever type of workflow you prefer, these commands are very well suited to be included in shell scripts. When a script is triggered by a triggered cron Job can help automate routine housekeeping tasks like deleting unwanted log files. As you investigate this use case, think about the power of these commands, test everything carefully, and always keep an up-to-date backup.

Linux commands
Filestar · pv · cat · tac · chmod · grep · difference · sed · With · man · pushed · popd · fsck · Test disk · seq · fd · pandoc · CD · $ PATH · awk · join · jq · wrinkles · unique · Journalctl · tail · stat · ls · fstab · echo · fewer · chgrp · chown · rev · look · Strings · Type · rename · Postal code · unzip · assemble · ummount · To install · fdisk · mkfs · rm · rmdir · rsync · df · gpg · weather · Nano · mkdir · from · ln · Patch · Convert · rclon · Scraps · srm
Processesalias · screen · above · kind · renice · progress · strace · system · tmux · chsh · story · at · Batch · for free · which · dmesg · chfn · User mod · ps · chroot · xargs · tty · pinkie finger · lsof · vmstat · Time out · Wall · Yes sir · kill · sleep · sudo · it is · Time · groupadd · User mod · groups · lshw · switch off · start anew · Stop · switch off · passwd · lscpu · crontab · date · bg · fg
Networkingnetstat · Ring · Trace route · ip · ss · who is · fail2ban · bmon · she · finger · nmap · ftp · curl · wget · who · who am I · w · iptables · ssh-keygen · ufw

Best Linux Laptops for Developers and Enthusiasts

Related Posts