Sudo bash -c 'echo data text > /path/to/file' Sudo - bash -c 'echo data > /path/to/file' Sudo - sh -c "echo 'text foo bar' > /path/to/file" See tee command man page by typing the following man command man tee Method 2: Use bash/sh shell
Whether you’re using Bash or any other shell, Linux provides flexible and powerful commands for you to delete directories and files straight from the terminal command line. In the following example the command that is passed to rmdir is: rmdir -p work/invoicesīoth the invoices and the work directories are deleted, as requested. That directory should now be empty, so it can be deleted by rmdir, and the process repeats stepping back up the path that was provided to rmdir. This trick works because rmdir starts with the target directory and then back-steps to the parent. You can use the -p (parents) option to delete a directory and to delete its parent directories too. rmdir -ignore-fail-on-non-empty work/reports /work/quotes This is an empty folder, and rmdir deletes it.
The -ignore-fail-on-non-empty option forces rmdir to ignore the error and move on to the next folder it needs to process, which is work/quotes. The work/reports folder has files in it, so rmdir cannot delete it. The -ignore-fail-on-non-empty option has been included in the command. In the following example two folders have been passed to rmdir, these are work/reports and work/quotes. You can force it to ignore these errors with the -ignore-fail-on-non-empty option so that other directories are processed. If you’ve asked it to delete four directories and the first one had files in it, rmdir would give you the error message and do nothing more. When rmdir gives a “Directory not empty” error, it stops processing the directories that were passed to it on the command line. The projects directory is left exactly as it was and the files in it are untouched. In the following example rmdir successfully, and silently, deletes the clients directory but it refuses to delete the projects directory because it contains files. If you try to delete a folder that is not empty, rmdir will give you an error message. It will never delete files.ĭelete a directory not in the current directory by specifying the full path to that directory: rmdir /path/to/directory The difference between rm and rmdir is that rmdir can only delete directories that are empty. There is another command, called rmdir, that you can use to delete directories.
Consult the command’s manual page for more information. If you get something wrong, you could accidentally delete all your system files. The rm command also has -one-file-system, -no-preserve-root, -preserve-root options, but those are only recommended for advanced users. You can also supply a path to the tree command to cause it to start the tree from another directory in the file system. Running the tree command produces a simple to understand diagram of the directory structure and files beneath the directory from which it is run. On other Linux distributions, use your Linux distribution’s package management tool instead.
Use apt-get to install this package onto your system if you’re using Ubuntu or another Debian-based distribution.