copy/move multiple files using cp/mv without using regular expressions
If you want to move or copy all of the files to the same directory, you can use the -t
option of cp
or mv
, but this will mean that you have to type/supply each filename as an argument. It works in the following manner, with as many files as arguments as you like:
cp -t /destination/directory/ file1 file2 file3
or
mv -t /destination/directory/ file1 file2 file3
This is quite laborious, but typing the file names can be made easier using Bash's tab completion.
Alternatively the following bash script will find all the files in a directory, given as the first argument, and copy the selected files into the destination directory, given as the second argument.
It checks each file and asks whether you want to copy that file. At the end of file selection it shows a list of selected files and asks if you want to copy them to the destination directory:
#!/bin/bash
directory=$1
destination=$2
selected_files=()
for f in ${directory}/*
do
if [[ -f $f ]]
then
while true
do
read -p "Would you like to copy ${f}? y/n: " choice
case $choice in
y|Y) selected_files+=("$f");
break ;;
n|N) echo "${f} will not be copied.";
break ;;
*) echo "Invalid choice, enter y/n: " ;;
esac
done
fi
done
echo "The following files will be copied to ${destination}."
for file in "${selected_files[@]}"
do
echo "$file"
done
while true
do
read -p "Are these the correct files? y/n: " confirm
case $confirm in
y|Y) break ;;
n|N) echo "Exiting filechooser"; exit 1 ;;
*) echo "Invalid choice, enter y/n: " ;;
esac
done
cp -t "$destination" "${selected_files[@]}"
Be warned that there's no error checking in this script about whether the destination directory exists, or that you've input the correct arguments.
Here's a script that selects a random set of files/directories to be copied. It can deal with arbitrary file names, even those containing newlines and spaces. Save the script as ~/bin/randomCopy.sh
, make it executable (chmod a+x ~/bin/randomCopy.sh
) and then run it, giving it the source directory as the first argument, the target directory as the second and the number of files/dirs (the script doesn't differentiate between files and directories, as you requested) to be copied. For example, to copy 5 random files or directories from /foo
to /bar
:
randomCopy.sh /foo /bar 5
The script:
#!/bin/bash
if [ $# -lt 3 ]; then
cat<<EOF
This script needs at least 3 arguments: the source directory, the
target directory and the number of files/dirs to be copied. For example:
$0 /from /to 5
EOF
exit
fi
sourceDir="$1"
targetDir="$2"
number="$3"
## Collect all file and directory names. The globstar
## bash option lets ** match all files and directories
## recursively
shopt -s globstar
dirsAndFiles=( ** )
## Get $num random numbers from 0 until
## the number of files and dirs found. This
## will let us take a random selection.
limit=$((${#dirsAndFiles[@]}-1))
numbers=$(shuf -i 0-"$limit" -n "$number")
for num in $numbers; do
cp -rv "${dirsAndFiles[$num]}" "$targetDir"
done
Note that this will overwrite existing files if any with the same file name exist in the target directory.
Maybe try using something like Midnight Commander? It is a console application that provides similar features to the graphical Nautilus File Manager.