copy/move multiple files using cp/mv without using regular expressions
Solution 1
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.
Solution 2
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.
Solution 3
Maybe try using something like Midnight Commander? It is a console application that provides similar features to the graphical Nautilus File Manager.
Related videos on Youtube
MikhilMC
Updated on September 18, 2022Comments
-
MikhilMC almost 2 years
Suppose I have a folder containing some files and some folders(files can be of any type). And I want to move/copy some of those files and folders using
mv
/cp
command. Is there any way, such that I could select some of them randomly, just as we do selection using the Ctrl key, and do move/copy using terminal? I can't use neither wildcard, nor the regular expression, as I want to select files of different types, and their names have small amount of similarities.-
Sergiy Kolodyazhnyy almost 8 yearsYou might want to provide more info. What kind of similarity files have ?
-
neocpp almost 8 yearsEven if you can't use regular expressions or wildcards, you can maybe take advantage of similarity and braces to expand arguments. In bash anyways if you have file1, file2, file3 as in one of the answers below, you can just use
cp -t /dest/ file{1,2,3}
or for rangescp -t /dest/ file{1..3}
. It works for non-numeric args and combinations too, e.g.cp -t /dest/ f{ile,ly}.{csv,txt}
executescp -t /dest/ file.txt file.csv fly.txt fly.csv
. Be careful with overwriting things if position is important, but I find them useful. -
Braiam almost 8 yearsWhy do you need this? Why not using the file manager as you describe? The shell tools tries to be the less ambiguous as possible.
-
MikhilMC almost 8 yearsWe all know we can select random files and folders under a folder using the Ctrl key. I just want to know whether we can implement a similar technique while using Linux terminal
-