You will learn during this step by step guide, the way to use the rm, unlink and rmdir commands to delete files and directories in UNIX system.
How to take away Files
You can use rm (remove) or unlink command to get rid of or delete a file from the UNIX system command. The rm command permits you to get rid of multiple files right away. With unlink command, you'll delete solely one file. While removing files or directories you ought to be additional careful, as if the file is deleted, you won’t be able to recover it simply.
1.Use the rm or unlink command to delete one file followed by the file name:
unlink /home/admin/directoryname myfile
rm /home/admin/directoryname/myfile
You will be notified for confirmation if the file is write-protected, as shown below. kind y and hit Enter key to get rid of the file, or else, it'll be deleted while not prompting if the file isn't write-protected.
Output |
rm: remove write-protected regular empty file ‘file’? |
Use the rm command to delete multiple files promptly followed by the file names separated with space:
rm /home/admin/directoryname/myfile2 rm /home/admin/directoryname/myfile3 |
OR
cd /home/admin/directoryname rm myfile1 myfile2 myfile3 |
OR
rm /home/admin/directoryname/myfile*
|
To match multiple files, you'll use Associate in Nursing asterisk (*) and regular expansions. Execute the subsequent command to get rid of all .pdf files placed within the current directory:
rm /home/admin/directoryname/*.pdf
|
Run the ls command to list the files if you're victimisation regular expansions so you'll be able to see what files are deleted before running the rm command.
Run the rm command with the -i choice to verify every file before deleting it:
rm -i /home/admin/directoryname/myfile(s)
|
You can run the -f (force) choice with rm command to get rid of the files while not prompting though the files area unit write-protected:
rm -f /home/admin/directoryname/myfile(s) |
You can additionally mix rm choices. you'll be able to run the subsequent command to delete all .txt files within the gift directory while not a prompt in tedious mode.
rm -fv /home/admin/directoryname/*.txt
|
How to take away Directories (Folders)
You can delete/remove directories (folders) by victimisation the rmdir and rm command in UNIX operating system.
rmdir may be a command-line perform that deletes empty directories/folders whereas rm command deletes directories and their contents recursively.
Run either rmdir or rm -d command to get rid of Associate in Nursing empty directory followed by the directory name:
rmdir /home/admin/directoryname rm -d /home/admin/directoryname |
Use the rm command with the -r (recursive) choice to delete non-empty directories and every one the files within them:
rm -r /home/admin/directoryname
|
You will be prompted to substantiate the deletion if a directory or a file within the directory is write-protected.
Run rm command with the -r (recursive) and -f choices to delete non-empty directories and every one the files while not being prompted:
rm -rf /home/admin/directoryname
|
Run the rm -r command followed by the directory names separated by area to delete multiple directories right away.
cd /home/admin/directoryname rm -r directoryname1 directoryname2 directoryname3 |
You can additionally use the asterisk (*) and regular expansions to match multiple directories constant like files.
Conclusion
By currently you're awake to the way to use the UNIX rm, rmdir and unlink commands to delete files and directories safely with the command.