how to remove files and directory on linux and unix
how to remove files and directory on linux and unix we are going to discuss mainly about how to delete bulk files and directory in linux and unix. There may be many scenario when you have need of deleting files and directory due to many business or job requirement.So you must be aware about how to remove files and directory on linux and unix.
What exactly we are trying to discuss here is special scenario like is as below.When we may need to remove files and directories on linux and unix.
Scenario 1 : If /var directory is full in linux and unix
Many cases /var file system gets full rapidly.If you check with df -h command it does not reflect that /var file system is really full.Then what ? It is full due to inode usage reached 100% and system stops writing any more files in /var file system which leads to all jobs failure which is using /var for writing anything.All logs also gets created in /var file system.
Table of Contents
How to check inodes in Linux
Procedure for how to check inodes in linux is as below. [root@linsrv01 ~]# df -i /var Filesystem Inodes IUsed IFree IUse% Mounted on /dev/sda5 525200 9159 516041 2% /var
How to check inodes in Solaris
Process for how to check inodes in unix is as below. [root@solsrv01 ~]# df -oi /var Filesystem iused ifree %iused Mounted on /dev/vx/dsk/bootdg/var 52637 943267 5% /var
So now we have fair idea how to check inode status of file system in Linux and Solaris. Many times not always mail system like sendmail or postfix creates too many files and it fills up inodes in /var/spool/clientmqueue.
Scenario 2 : Application FS is full in linux and unix
If any application file system for instance Control-M which is enterprise job scheduling tool mostly used in production and dev environment .Generally for instance it have file system like /export/opt/ctmagent.
Control-M have sysout directory where all the job logs getting placed. Due to very frequent nature of job logs inode gets filled very drastically. If it is not getting address it may import all your job scheduled as control-m jobs will fail as it does not have free inode in file system to write sysout.
Normally if you check messages files in Linux (/var/log/messages) or in Solaris (/var/adm/messages) you will see error messages like below.
Dec 19 00:03:32 solsrv01 vxfs: [ID 702911 kern.notice] NOTICE: msgcnt 3644 mesg 001: V-2-1: vx_nospace – /dev/vx/dsk/fs-general-dg/vol001_8GB file system full (1 block extent)
Dec 19 00:03:39 solsrv01 vxfs: [ID 702911 kern.notice] NOTICE: msgcnt 3645 mesg 001: V-2-1: vx_nospace – /dev/vx/dsk/fs-general-dg/vol001_8GB file system full (1 block extent)
Dec 19 00:03:48 solsrv01 vxfs: [ID 702911 kern.notice] NOTICE: msgcnt 3646 mesg 001: V-2-1: vx_nospace – /dev/vx/dsk/fs-general-dg/vol001_8GB file system full (1 block extent)
In such situation normally listing system hangs or even if you try to rm session gets closed or you may get “Argument list is too long” if you try to remove files in bulk .Usually it contains many thousands of files and it critical as if you try to remove files manually it may take too long.
Lets deep dive into deletion more which was our original topic .Lets see some normal examples.
How to delete single file in linux and unix
1.Just go to the location where file exists and remove file. # cd <path=/dir/location> # rm <file name> OR use absolute path to delete file at once # rm <file absolute path >
How to delete multiple files in linux and unix
In this case as well either you have to go to location where file exists or you can use absolute path. # rm <filename1> <filename2> <filename3>
Linux Rename File
You can linux rename file using mv or cp command.Advantage with cp is it will still leave original file
intact while mv will completely move file to different name and original file will no more exist.
#cp abc.txt <new_name.txt> ---After command abc.txt will still intact.
#mv abc.txt xyz.txt ---After command there will be no abc.txt anymore.
These commands can be used using absolute path as well.That,s it about linux rename file.
How to Linux Remove Directory
How to linux Remove Directory ? You have to go to location where directory exists or you can use absolute path. # rm -rf <directory> # rm -rf <directory absolute path> Where r is referenced to recursive and f is for forcible which clearly means you can remove entire directory including it,s sub-directory.
Finally lets see
How to delete bulk files and directory in linux and unix
This will be really helpful in scenarios mentioned above to fix inode full condition otherwise you have long procedure to address those issue if you try to increase number of inodes in existing file system.You may loose data and end-up restoring data from tapes.
Step 1 : Change to the directory where you have to remove files Step 2 : Run for loop to get files of specified date for example you can divert output of script to any other location. Use below one liner for loop to find files of specific date. for i in `ls -lh | grep -i "Dec 19" | awk '{print $9}'` ; do ls -ld $i ; done Step 3 : As mentioned above saving output to any file for your review before removal. Please make sure these are the files you want to remove. Use same loop and change command to rm -rf like below. for i in `ls -lh | grep -i "Dec 19" | awk '{print $9}'` ; do rm -rf $i ; done
Once it comes back to prompt, you are done or you may need to run various dates files using above two for loop commands one is listing for review another removal. This is all about how to delete bulk files and directory in Solaris and Linux.
Warning : RM is the one of the most dangerous command in all distribution. You have to be very careful while executing while using rm command one space may do blunder. Make sure you understand what are you deleting as there is no redo kind of things.You will left with only option to restore file using backup. Command or script used in this post is just to share how to fix issues discussed above .These are tested many a times but advised to use it on your own risk and site owner can not be help responsible.
Comments
Pingback: How to disable IP forwarding in linux – nixDrafts - UNIX Linux Tips, UNIX Linux how to steps
Pingback: How to change DG name in the Veritas Volume Manager – RSY DIGITAL WORLD
Pingback: All about Unix Linux System Administrator Roles and Responsibilities – RSY DIGITAL WORLD