Note: To understand the meaning of the flags employed in the commands above, refer to the man pages of each command we have used in the various illustrations. Now run the command to clean up the directory: $ rm -v *Īfterwards, turn off GLOBIGNORE variable: $ unset GLOBIGNOREĭelete Files Using Bash GLOBIGNORE Variable txt files with be removed from the current directory. To employ this method, move into the directory that you wish to clean up, then set the GLOBIGNORE variable as follows: $ cd test Suggested Read: Useful 12 Practical Examples on Grep Command in Linux Here, the GLOBIGNORE variable stores a colon-separated pattern-list (filenames) to be ignored by pathname expansion. This last approach however, only works with bash. Remove All Files Except File Extensions Delete Files Using Bash GLOBIGNORE Variable type f -not \(-name '*gz' -or -name '*odt' -or -name '*.jpg' \) -delete jpg files in the current directory: $ find. Let us look at one additional example, the command below will wipe out all files excluding. Remove Files Using find and xargs Commandsħ. $ find /directory/ -type f -not -name 'PATTERN' -print0 | xargs -0 -I ![]() Under this method, we can use find command exclusively with appropriate options or in conjunction with xargs command by employing a pipeline as in the forms below: $ find /directory/ -type f -not -name 'PATTERN' -delete Once you have all the required commands, turn off the extglob shell option like so: $ shopt -u extglob odt files as follows, while displaying what is being done: $ rm -v !(*.zip|*.odt)ĭelete All Files Except Certain File Extensions Next, you can delete all files in a directory apart from all. zip files interactively: $ rm -i !(*.zip)ĭelete All Files Except Zip Files in LinuxĤ. The example below shows how to remove all files other than all. To delete all files with the exception of filename1 and filename2: $ rm -v !("filename1"|"filename2")ĭelete All Files Except Few Files in Linuxģ. To delete all files in a directory except filename, type the command below: $ rm -v !("filename")ĭelete All Files Except One File in LinuxĢ. To use them, enable the extglob shell option as follows: # shopt -s extglobġ. !(pattern-list) – matches anything except one of the given patterns.– matches one of the specified patterns. ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |