Home IT Linux Windows Database Network Programming Server Mobile  
           
  Home \ Linux \ Delete specific files using bash directory under Linux     - MariaDB phpMyAdmin installation and configuration issues to resolve under CentOS7 (Database)

- Puppet 3.x installed on Debian 7 (Server)

- CentOS / Linux NIC set IP address configuration (Linux)

- Enterprise Hadoop cluster architecture - DNS installation (Server)

- Those functions under Linux you do not know the df command (Linux)

- Oracle 12C RAC optimizer_adaptive_features cause of data into overtime (Database)

- Python several standard types of built-in functions (Programming)

- A summary of Java multi-threaded programming - acquaintance multithreading (Programming)

- MySQL Basic tutorial: About varchar (N) (Database)

- Teach you the Ubuntu Server enabled SSH multifactor authentication (Server)

- Development environment to build MEAN In Ubuntu 15.10 (Server)

- How to troubleshoot Windows and Ubuntu dual system time is not synchronized (Linux)

- Ubuntu Gitolite management Git Server code base permissions (Server)

- JavaScript: understanding regular expressions (Programming)

- CentOS minimal network is unavailable resolved (Linux)

- MySQL query optimization: profile function (Database)

- How to Install Redis server on CentOS 7 (Server)

- Database start listening TNS-12537, TNS-12560 error (Database)

- Simple steps allows you to build a more secure Linux server (Linux)

- Circular list of Java programming (Programming)

 
         
  Delete specific files using bash directory under Linux
     
  Add Date : 2018-11-21      
         
       
         
  I am a new Linux user. Now I need to clean up a download files in the directory, in fact, I just want to be deleted in addition to the following file formats so other files from ~ / Download / folder:

* .iso - All iso file format.
* .zip - All zip format.
How do I based on Linux, bash shell on OS X or Unix-like systems remove specific documents?

Bash shell supports various file pattern matching characters such as:

* - Matches all files.
? - Match the file name of a single letter.
[...] - Matches any closed parenthesis letter.
Strategy # 1: knowledge about the extended pattern-matching character

Here you need to use the system built shopt command to open the shell extglob option, then you can use extended mode character, and these pattern-matching characters as follows:

? (Pattern list) - Matches zero or a given pattern.
* (Pattern list) - Matches zero or more times a given pattern.
+ (Pattern list) - At least one match a given pattern.
@ (Pattern list) - match a given pattern.
! (Pattern list) - does not match a given pattern.
A list of patterns that use one or more | separated mode (file name).

First open extgolb options:

shopt -s extglob
Delete all files except .iso and .zip files in Bash

Rm command syntax is:

## Remains only file1 file ##
rm! (file1)
## Retaining only file1 and file2 files ##
rm! (file1 | file2)
## ## Retaining only zip files
rm! (*. zip)
## To retain only ## zip and iso files
rm! (* zip |. * .iso)
## You can also use the full directory ##
rm /Users/vivek/!(*.zip|*.iso|*.mp3)
## Can also pass parameters ##
rm [options] (* zip |. * .iso)!
rm -v (* zip |. * .iso)!
rm -f! (* zip |. * .iso)
rm -v -i! (*. php)
Finally, close extglob options as follows:

shopt -u extglob
Strategy # 2: Use the bash variable GLOBIGNORE delete all the files other than the specified file

From bash (1) man page:

This is a colon-separated list of patterns defining the set of files to be ignored by Path expansion mode. If a file path to match the flat pattern also matches GLOBIGNORE the mode, then it will be removed from the list of matches.

To remove all files retain only the zip and iso files should be set as follows GLOBIGNORE:

Only ## of the possible ## in BASH
cd ~ / Downloads /
. GLOBIGNORE = * zip: * iso.
rm -v *
unset GLOBIGNORE
Strategy # 3: Delete all other files using the find command retains only zip and iso files

If you are using tcsh / csh / sh / ksh or other shell, you can try using the following syntax find commands on Unix-like systems to delete this file:

find / dir / -type f -not-name 'pattern matching' -delete
or

## For weird file name can use xargs ##
find / dir / -type f -not-name 'pattern matching' -print0 | xargs -0-I {} rm {}
find / dir / -type f -not-name 'pattern matching' -print0 | xargs -0-I {} rm [options] {}
I want to delete the file ~ / source directory except php, type:

find ~ / sources / -type f -not-name '* .php'-delete
or

find ~ / sources / -type f -not-name '* .php'-print0 | xargs -0-I {} rm -v {}
Reserved only * .zip and * .iso file syntax is as follows:

find.-type f -not \ (-name '* zip'-or-name' * iso '\) -delete
     
         
       
         
  More:      
 
- SLF4J Tutorial (Programming)
- MySQL loose index scan (Database)
- Fundamentals of the Java virtual machine memory management principles (Programming)
- Oracle lag () and lpad () function (Database)
- 3 ways to create a lightweight, durable system of Ubuntu Linux USB disk (Linux)
- Rely on IP chain established Linux firewall (Linux)
- Use in Linux ipmitool tool (Linux)
- How to set the default Fedora from the command line (Linux)
- Python configuration tortuous road of third-party libraries Numpy and matplotlib (Programming)
- Ora-00439: feature not enabled: managed standby (Database)
- Android Get App version number and version name (Programming)
- JavaScript prototype and the prototype chain (Programming)
- Linux systems dmesg command processing failures and system information collected seven usage (Linux)
- Linux - EXT2 file system is described in detail (Linux)
- IP Security Policy is to learn how to prevent Ping and closed ports (Linux)
- Ubuntu 14.04 installed NVIDIA graphics driver (Linux)
- Linux environment variable configuration and save places (Linux)
- Using Lua implement various operations list (Programming)
- Linux script commands - terminal recorder (Linux)
- Grub2: Save Your bootloader (Linux)
     
           
     
  CopyRight 2002-2016 newfreesoft.com, All Rights Reserved.