bash: read a line from file
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.
Are there spaces in the full filenames?
If not, this should work:
for filename in `cat /path/to/filelist` ; do
cp $filename /path/to/new/location/
Are there hundreds of files? If not, this would work and is more simple:
cp `cat /path/to/filelist` /path/to/new/location/
- 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.
Unfortunately there are spaces in filenames. In fact, I don't think there is even a single filename without spaces :(
One more thing: how do I detect the suffix of file? I need something like this:
if [filename = *.txt]; then
The syntax in the if sentence test condition is probably incorrect, how do I do it right?
try this (if i understand your question)
while read i
echo $i # or whatever you want
done < filename
Thanks, that works perfect! Do you also know how could I check the suffix?
Nevermind, I managed to check the suffix with this:
if [ "$(basename "$i" .txt)" != "$(basename "$i")" ]; then
echo File \"$i\" has suffix .txt
Good thing I tok another look at man basename :)
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` ;
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
it must have been a stupid error, because now it works fine
|All times are GMT -5. The time now is 05:15 AM.|