

Remember, the > operator replaces the existing contents of the file with the output of the command.

For example, the cat command prints the contents of a file to the terminal: cat /path/to/file If you view the contents of the file, you’ll see the ls command’s output. Specify any valid path and bash will create a file at that location. You learned about the rm and rmdir commands and when to use each.You don’t have to specify the path to an existing file.
#BASH PASTE TO FILE HOW TO#
Now you know how to delete directories in Linux from the command line. Learn more in the globbing documentation Wrap Up The asterisk * matches any mixture of characters after the "test" word. You can use a wildcard glob pattern like this: rm -r test* Globbing is similar to Regex, but the former is used for matching file names in the terminal.įor example, if you want to delete the directories test1, test2, and test3, instead of running: rm -r test1 test2 test3 You can also use rm and rmdir with glob patterns. How to Delete Directories that Match a Pattern in Linux But this command will throw an error if either directory is not empty. This command will delete Test2 and afterward delete Test, the parent in the tree. You can use the -p flag like this: rmdir -p Test/Test2 So instead of doing: rmdir Test/Test2 Test

If you delete the Test2 directory, Test becomes an empty directory. In this case, Test is a directory that has the Test2 subdirectory. For example, if you have this file structure: > Test The rmdir command also has the -p flag, which allows you to delete a directory along with its parent in the tree. To delete an empty directory, use this command without options: rmdir test When you use rmdir on a non-empty directory, you get this error: rmdir: : Directory not empty. It is the equivalent of the rm command with the -d flag: rm -d. The rmdir command is specifically used to delete empty directories. So, to be sure you are performing the proper empty directory operation, use the -d flag. If it is not empty, you will get the error rm: test: Directory not empty. It is recommended to use the -d flag for empty directory cases instead of the -r flag because the -d flag ensures that a directory is empty. To delete an empty directory, you can use this command: rm -d test Without this flag, you will get the same error rm: : is a directory. So, you can delete a directory like this: rm -r testįor an empty folder, you can still provide the -r flag, but the dedicated -d flag applies to this case. The -r flag informs the rm command to recursively delete the contents of a directory (whether it's files or subdirectories). You will get this error: rm: test: is a directory Without using this flag like this: rm test How to delete a folder with contentsįor a directory with contents, you have to provide the -r flag. To remove a file, say test.txt, you can use the command without options like this: rm test.txtįor directories, you have to provide some flag options. For directories, this command can be used to delete a directory entirely – that is, it deletes a directory and all files and subdirectories within the directory. You use the rm command to delete files and directories in Linux. Let's look at both commands in more detail. The TL DR of both commands is that rm deletes directories that may contain content such as files and subdirectories, while rmdir ONLY deletes empty directories.Īlso, both commands delete directories permanently (rather than moving them to the trash), so be careful when using them. There are two ways to remove directories in Linux: the rm and rmdir commands. But how do you do this on the terminal? I'll explain that in this article. If you're using a user interface, you can right-click on a directory and select "Delete" or "Move to Bin".
