Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Move all files except one on Linux
When working with Linux, you may need to move multiple files from one directory to another while keeping specific files in their original location. This is a common task for file organization, cleanup operations, or selective backups. Linux provides several methods to accomplish this using different command-line approaches.
Renaming The Unwanted File
This method involves temporarily renaming the file you want to keep by adding a dot prefix, making it a hidden file. The mv command with wildcards will ignore hidden files, allowing you to move everything else.
# Rename the file to keep as hidden mv file5 .file5 # Move all visible files to target directory mv * ~/target_dir/ # Verify only the hidden file remains ls -la
total 0 drwxrwxr-x 2 ubuntu ubuntu 60 Jun 10 03:42 . drwxr-xr-x 21 ubuntu ubuntu 520 Jun 10 03:25 .. -rw-rw-r-- 1 ubuntu ubuntu 0 Jun 10 00:57 .file5
After moving the files, restore the original filename:
mv .file5 file5
Using Extended Globbing with Negation
Extended globbing allows you to use pattern negation with the !(pattern) syntax. This directly excludes specified files from the operation without renaming.
# Enable extended globbing shopt -s extglob # Move all files except the specified one mv !(file5) ~/target_dir/
You can exclude multiple files by separating them with pipe symbols:
mv !(file1|file2|file5) ~/target_dir/
Using Inverted ls Search
The ls command's -I option ignores specified patterns. Combined with command substitution, this creates a filtered file list for the move operation.
# Using command substitution with $() mv $(ls -I file5) ~/target_dir/ # Alternative using backticks (deprecated) mv `ls -I file5` ~/target_dir/
For processing each file individually, use xargs:
ls -I file5 | xargs -I {} mv {} ~/target_dir/
Using Inverted Grep Search
This approach combines ls with grep -v to filter out unwanted files. The -v flag inverts the match, showing all files except the specified one.
# Using command substitution
mv $(ls | grep -v file5) ~/target_dir/
# Using xargs for individual processing
ls | grep -v file5 | xargs -I {} mv {} ~/target_dir/
This method is particularly useful when you need pattern matching capabilities:
# Exclude files matching a pattern mv $(ls | grep -v "\.log$") ~/target_dir/
Comparison of Methods
| Method | Advantages | Disadvantages |
|---|---|---|
| Renaming | Simple, works with any shell | Requires two extra commands |
| Extended Globbing | Clean syntax, supports multiple exclusions | Requires bash, needs extglob enabled |
| ls with -I | Straightforward, supports wildcards | Limited pattern matching |
| grep -v | Powerful pattern matching, flexible | More complex syntax |
Best Practices
Test first Use
lsto preview which files will be moved before executing themvcommand.Use absolute paths Specify full paths to avoid confusion about source and destination directories.
Handle spaces Quote filenames or use
-print0withxargs -0for files with spaces.Backup important files Always backup critical data before performing bulk move operations.
Conclusion
Linux offers multiple approaches to move files while excluding specific ones, each with distinct advantages. Extended globbing provides the cleanest syntax for bash users, while grep-based filtering offers the most flexibility for complex patterns. Choose the method that best fits your shell environment and specific requirements.
