Linux - GeneralThis Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
I need to copy a lot of files, all are listed in a text file, one filename (with full path) per line.
What I'm missing is a command or something that would read that file line by line so I could make a loop and pass filename (i.e. current line read) to the cp command.
BTW, is there a C or C++ function for copying files, or is that usually done with system("cp whatever whatever") or even reading source file, creating and writing to outfile? I'm more familiar with C than bash scripting, that's why I thought of that, but I'd prefer a more elegant solution.
If not, this should work:
for filename in `cat /path/to/filelist` ; do
cp $filename /path/to/new/location/
done
Are there hundreds of files? If not, this would work and is more simple:
cp `cat /path/to/filelist` /path/to/new/location/
Notes:
- in both cases, this kind of copying will not preserve directory structure, all files will be copied into one single directory, so, if there are files with the same short name (i.e. without path), they will owerwrite each other
- if there may be spaces in filenames, the above examples will not work.
So watch out.
sorry for the digging butt I'm having a similar problem
i need to read a file line at line, and echo the line itself,
i know it's stupid but for now is what i want for the big picture.
i tried something like this
for filename in `cat file` ;
do
echo $filename
done
and it seems to print every word in the file, separating them by spaces, not by new lines.
i will need this because i'm trying to compare 2 files, that have a line that starts with a number, so i can match it on both files.
for now my code is something like this
[bin]# vi compare.sh
Quote:
#!/bin/bash
# global vars
mallocs=$1
frees=$2
tempValue=0
# function to validate mtu size
function compareFiles
{
exec<$mallocs
while read line
do
tempValue=`expr $tempValue + 1`;
echo $tempValue;
done
echo "**** $tempValue";
# go to malloc file, open it and read each line and get the number
# for i in $`cat $mallocs`;
# do
# echo "-> $i "
# done
# match the number with second file
# not found ? print to bash
# found, go to next number in mallocs file
}
# script sequence
if [ $# -lt 2 ]; then
echo "Invalid number of parameters. Required 2 parameters"
echo "parameter 1: filename of mallocs"
echo "parameter 2: filename of frees"
else
compareFiles
fi
edit:
it must have been a stupid error, because now it works fine
final result
Quote:
#!/bin/bash
# global vars
mallocs=$1
frees=$2
# function to validate mtu size
function compareFiles
{
# go to malloc file, open it and read each line and get the number
exec<$mallocs
while read line
do
#mytemp will store the number in the line, we know the number is in the first position
MYTEMP=$(echo $line | cut -f 1 -d ' ')
#retvalue will store the result of the comparisson between
#mytemp and all the values in the free files
RETVALUE=`cat $frees | grep $MYTEMP`
if [ "$RETVALUE" == "" ]; then
# not found means it's not on both files, print it
echo $MYTEMP
fi
done
}
# script sequence
if [ $# -lt 2 ]; then
echo "Invalid number of parameters. Required 2 parameters"
echo "parameter 1: filename of mallocs"
echo "parameter 2: filename of frees"
else
compareFiles
fi
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.