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.
The error you are getting is because you are reading /trial/file1.txt
and not ./trial/file1.txt
. That means the shell is trying to find a directory called trial
which is under the root directory (/
). If you want a path that is relative to your current directory, you can just use:
while read file; do cp "$file" trial/test_folder; done < trial/file1.txt
Or,
while read file; do cp "$file" ./trial/test_folder; done < ./trial/file1.txt
Or, you can use the full path:
while read file; do cp "$file" /home/shane/trial/test_folder; done < /home/shane/trial/file1.txt
Best Answer
Assuming
list.txt
has names of files (with only prefix) one in each line, the following code will move all listed files tofolder2
: