Learn through the super-clean Baeldung Pro experience:
>> Membership and Baeldung Pro.
No ads, dark-mode and 6 months free of IntelliJ Idea Ultimate to start with.
Last updated: March 16, 2024
In Linux, manipulating files is an important task. From beginners to experienced system administrators, moving files from one location to another is a common operation. One scenario that might arise is moving a specific number of files from a directory.
In this tutorial, we’ll explore how to efficiently move the first n files from one directory to another.
Before we proceed, let’s note that we’ll use projects as the source directory that contains the files to move. Meanwhile, we’ll use the destination directory as the directory to move the first n files to.
Moving files using the graphical user interface in Linux entails working with a file manager like Nautilus in GNOME, or some other, depending on the installed desktop environment. This approach is practical when dealing with only a few files since moving many files manually is inefficient. Also, it’s convenient for beginners since it allows them to visually interact with the files they intend to move.
To begin, we need to open the file manager. In this case, we’ll demonstrate using Nautilus. After this, let’s navigate to the projects directory, which contains the files we’re interested in moving:
Next, let’s click on the first file we want to move, press and hold the Shift keyboard key, and then click on the last file we want to move. This selects a range of files from the first file to the last. However, if the files to move are not listed consecutively, we can hold down the Ctrl key and click on each file individually to select it.
Once we select all the desired n files, let’s right-click on one of the files and click on the Move to option from the menu that appears to begin the move. After selecting Move to, the file manager prompts us to choose the destination directory to move the selected files to:
Here, we navigate to the destination directory and click the Select button to move the selected files.
Alternatively, we can opt to use the command line. By using this approach, we can automate repetitive tasks and also move files efficiently when dealing with many files.
In this approach, we’ll use a combination of the mv, ls, grep, and head commands to move files from the current directory to a directory named destination:
$ mv $(ls -p | grep -v / | head -5) ../destination
Let’s break this command down:
The above command selects the first five files, excluding directories, and moves them to the destination directory one level above the current directory.
In this next approach, we’ll combine the find, sort, head, xargs, and mv commands:
$ find . -maxdepth 1 -type f | sort | head -5 | xargs -I {} mv {} ../destination
Here’s the breakdown:
Similar to the previous command, this command also looks for regular files in the current directory. It captures the first five and moves them to the destination directory.
Creating a Bash script ensures flexibility and automation in the case of repetitive tasks. To demonstrate, let’s create a script.sh file and paste this content:
#!/bin/bash
# Define the source directory
source_directory="./"
# Define the destination directory where files will be moved
destination_directory="../destination"
# Define the number of files to move
num_files=5
# Initialize a counter to keep track of the number of files moved
count=0
# Iterate over the files in the source directory
for file in "$source_directory"*
do
# Check if the regular file exists
if [ -f "$file" ]; then
# Move the file
mv "$file" "$destination_directory"
# Increment the counter
((count++))
# Check if the desired number of files have been moved
if [ "$count" -eq "$num_files" ]; then
echo "Moved $count files."
break
fi
fi
done
echo "Moving of files is complete."
Let’s break this down:
Next, let’s execute the Bash script:
$ bash script.sh
Moved 5 files.
Moving of files is complete.
When we run this script, we need to change the source_directory and destination_directory paths with the actual paths to our source and destination directories. Additionally, we need to modify the num_files variable to the exact number of files that we want to move.
In this article, we explored how to move the first n files. Whether we’re moving files for backup, organization, or any other reason, understanding how to move the first n files efficiently is valuable.
We discussed various approaches that we can use to accomplish the task at hand. These include using the graphical user interface, executing one-line commands from the command line, and creating Bash scripts for multi-line instructions and repetitive tasks. Every option is valid, and selecting which one to use will depend on our preferences and requirements.