Cat files listed in text file and redirect to new directory with same filename


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Cat files listed in text file and redirect to new directory with same filename
# 1  
Old 06-26-2015
Cat files listed in text file and redirect to new directory with same filename

I have a directory that is restricted and I cannot just copy the files need, but I can cat them and redirect them to a new directory. The files all have the date listed in them. If I perform a long listing and grep for the date (150620) I can redirect that output to a text file. Now I need to take that file and, using a script, cat all of the files listed and redirect the output to a temp directory with the same filename.

I've tried several thing but keep getting errors. I've VERY new to scripting and any help would be appreciated.
# 2  
Old 06-26-2015
Hi

You probably want:
Code:
cat "thisfile" > "/tmp/newfile"

or
Code:
grep 150620 "$thisfile" > "/tmp/newfile"

Note that > will overwrite newfile with the the content of thisfle.
To append thisfile to newfile, use >>.

hth
# 3  
Old 06-26-2015
I realized the problem I was creating. I forgot about all of the extra items in the long listing. I figured it out, of course after I posted for help.


This worked for me.
Code:
For I in $(ls /sourcedir | grep string)
Do
Cat /sourcedir /$i > targetdir/$i
Done


Last edited by Don Cragun; 06-26-2015 at 05:52 PM.. Reason: Change LEFT tags to CODE tags.
# 4  
Old 06-26-2015
This didnt work for you:
1) there are cap-chars.
2) you issue 'I' /$I) but are using 'i' ($i) (cap/noncap chars too)
# 5  
Old 06-26-2015
I'd be very surprised that you'd be denied to cp files but allowed to cat them, as both are just reading operations that need read permission. Are you sure you didn't try sth. different?
# 6  
Old 06-26-2015
Quote:
Originally Posted by trigger467
I realized the problem I was creating. I forgot about all of the extra items in the long listing. I figured it out, of course after I posted for help.


This worked for me.
Code:
For I in $(ls /sourcedir | grep string)
Do
Cat /sourcedir /$i > targetdir/$i
Done

In addition to the capitization errors already mentioned, having a space in the middle of a filenames guaranteed to give you the wrong results.

The command cat /sourcedir /$i tries to copy the contents of a directory (/sourcedir) and the contents of a (probably non-existent) file in the root directory (/$i). That is an illegal operation on a directory on many systems. I doubt that this worked for you (even if you used cat instead of Cat; but it would have created empty files in the destinations you wanted.

If I understand what you're trying to do (and I'm not at all sure that I do), the following might be a better approach:
Code:
for i in /sourcedir/*string*
do	cp "$i" "targetdir"
done

or maybe just:
Code:
cp /sourcedir/*string* "targetdir"

if the files to be moved don't overflow argument length limits.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Want to delete the junk files from a directory which are not listed in a TEXT file

Hello Everyone, I want to delete the image files from a directory, which are not listed in a TEXT file. The directory contains large number of image files (in millions) required / not required. I want to delete the image files which are "not required". I have generated a Text file having... (3 Replies)
Discussion started by: Praveen Pandit
3 Replies

2. Shell Programming and Scripting

Delete files listed in text file

Hi Team, Here's the scenario, I have a text file called "file_list.txt". Its content is as follows. 111.tmp 112.tmp 113.tmp 114.tmp These files will present in "workdir" directory. It has many files. But only the files present in file_list.txt has to be deleted from the workdir... (7 Replies)
Discussion started by: kmanivan82
7 Replies

3. Shell Programming and Scripting

perl Compare zone files in directory with what is listed in named.conf

I would really appreciate any assistance that I can get here. I am fairly new to perl. I am trying to rewrite my shell scripts to perl. Currently I have a shell script (using sed, awk, grep, etc) that gets a list of all of the zone files in a directory and then looks in named.conf for what... (0 Replies)
Discussion started by: brianjb
0 Replies

4. Shell Programming and Scripting

Copy files listed in text file to new directory

I am trying to write a script that will copy all file listed in a text file (100s of file names) to a new directory Assume script will run with main as current working directory and I know how many files/lines will be in List.txt Im trying to work up a test script using this model Contents of... (2 Replies)
Discussion started by: IAmTheGrass
2 Replies

5. Shell Programming and Scripting

Recursively cat files in a directory with filename printed first.

I want to recursively cat the content of files in a directory e.g. find /etc -type f -exec cat {} \; But I want it to print the file name first and then the content. For example let's say /etc/statetab and /etc/colord.conf will be printed first then I want the output to look something like; ... (6 Replies)
Discussion started by: lewk
6 Replies

6. UNIX for Dummies Questions & Answers

cat files from subdirectories output using same filename

Hi, I need to concatenate data files with a .mp extension that are stored in directories by year. I want to keep the same filename as an output for example: for the file name p030.mp, which resides in the following subdirectories: /2000/p030.mp /2001/p030.mp /2002/p030.mp I want to:... (4 Replies)
Discussion started by: cmshreve
4 Replies

7. Shell Programming and Scripting

cat redirect EOF missing text

Hello attempting to redirect out to create a startup script in solaris. The steps are working but the $1 entry is being left out. syntax below and content of output file below. cat > S99build << EOF > #!/bin/bash > case $1 in > 'start') > /usr/os-buildsol.sh > > ;; > esac > exit 0 >... (3 Replies)
Discussion started by: juanb25
3 Replies

8. Shell Programming and Scripting

Moving files listed in a data file to a new directory using Perl

Hi, I have a data file that lists a number of files. I want to move the files named in that one to another directory. Here's what I have: #!/usr/bin/perl -w open(FILE, "<collision.txt"); my @lines=<FILE>; foreach my $lines (@lines) { system("mv $lines collisions/."); } close(FILE); ... (2 Replies)
Discussion started by: renthead720
2 Replies

9. Shell Programming and Scripting

Shellscript to sort duplicate files listed in a text file

I have many pdf's scattered across 4 machines. There is 1 location where I have other Pdf's maintained. But the issues it the 4 machines may have duplicate pdf's among themselves, but I want just 1 copy of each so that they can be transfered to that 1 location. What I have thought is: 1) I have... (11 Replies)
Discussion started by: deaddevil
11 Replies

10. Shell Programming and Scripting

Copy files listed in a text file - whitespace problem.

Hi, Say I have this text file <copy.out> that contains a list of files/directories to be copied out to a different location. $ more copy.out dir1/file1 dir1/file2 dir1/file3 "dir1/white space" dir1/file4 If I do the following: $copy=`more copy.out` $echo $copy dir1/file1... (4 Replies)
Discussion started by: 60doses
4 Replies
Login or Register to Ask a Question

Featured Tech Videos