Appending the contents of a file at the beginning of another file in UNIX [closed] - linux

Closed. This question is off-topic. It is not currently accepting answers.
Want to improve this question? Update the question so it's on-topic for Stack Overflow.
Closed 10 years ago.
The community reviewed whether to reopen this question last year and left it closed:
Not suitable for this site This question does not appear to be about a specific programming problem, a software algorithm, or software tools primarily used by programmers. If you believe the question would be on-topic on another Stack Exchange site, you can leave a comment to explain where the question may be able to be answered.
Improve this question
I know that
cat file1 >> file2
would append the contents of file1 at the end of file2. On the other hand, how can I append the contents of file1 at the beginning of file 2, and not at its end?
Actually, i have a single master file M, and several other files in a directory D. I want to append the contents of file M at the beginning of all the files in the directory D.

Just do:
cat file1 file2 > tmp && mv tmp file2

For each file you could do:
cat MASTER file >> file.tmp
And then move file.tmp over file.

You will have to use a temporary file and rename it after merge.
Example:
echo -e "a\nb\nc" > LETTERS
echo -e "1\n2\n3" > NUMBERS
cat NUMBERS LETTERS > TMP
mv TMP LETTERS
cat LETTERS
Your command might look something like:
for file in $( find -name "*.java" ); do cat PREPEND ${file} > ${file}.tmp; mv ${file}.tmp ${file}; done

Related

How do I copy grep output to another text file in different directory? [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 2 days ago.
Improve this question
I am trying to copy specific words from a text file in a directory to another using grep. I have the retrieval of the words I want from the text file, now I just am wondering how I would go about moving it another text file, say in my home directory.
Here is the grep command.
grep -E '^.[*ing]{5}$' words
and here is what I have tried
grep -E '^.[*ing]{5}$' words > words.txt $home
grep -E '^.[*ing]{5}$' words > words.txt /home/
Any help is appreciated!
grep -E '^.[*ing]{5}$' words > /home/words.txt

What do terminal commands ls > wc and ls | wc show? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about a specific programming problem, a software algorithm, or software tools primarily used by programmers. If you believe the question would be on-topic on another Stack Exchange site, you can leave a comment to explain where the question may be able to be answered.
Closed 1 year ago.
Improve this question
I know what the commands ls and wc do, but I can not find out what ls > wc and ls | wc will show. Can someone please help me flush out the meaning of this commands?
ls | wc The output from the ls command is piped into the wc command. So it will count the words which are in the output of ls. So you see simply the number of files read by ls.
ls > wc This creates a new file in your current working directory with the name wc with the output of your ls command. The program wc is not used here, simply a new file with the same name is created. You can simply look into this new file with your favorite editor or simply use cat for it.

How to rename files with filename from one txt file to filename from another txt file in bash? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about a specific programming problem, a software algorithm, or software tools primarily used by programmers. If you believe the question would be on-topic on another Stack Exchange site, you can leave a comment to explain where the question may be able to be answered.
Closed 6 years ago.
Improve this question
I have some filename in a .txt file and some other filename in another .txt file. I want to rename files stored in that folder from filename in the text file line by line.
FROM :
$cat oldname.txt
file1.mp4
file2.mp4
TO :
$cat newname.txt
video1.mp4
video2.mp4
I want some bash script that can execute mv command line by line for each file.
Like
$mv file1.mp4 video1.mp4
Use a proper loop over the file to rename with bash . Open the files separately in different file-descriptors.
#!/bin/bash
while read oldname <&3 && read newname <&4
do
mv "$oldname" "$newname"
done 3<oldname.txt 4<newname.txt
Try:
while read oldname; do
read -u 3 newname
echo mv $oldname $newname
done < oldname.txt 3< newname.txt
This will merely echo the commands. If you like the result, omit the echo.

Count number of files within a directory in Linux? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about a specific programming problem, a software algorithm, or software tools primarily used by programmers. If you believe the question would be on-topic on another Stack Exchange site, you can leave a comment to explain where the question may be able to be answered.
Closed 9 years ago.
Improve this question
To count the number of files in a directory, I typically use
ls directory | wc -l
But is there another command that doesn't use wc ?
this is one:
ls -l . | egrep -c '^-'
Note:
ls -1 | wc -l
Which means:
ls: list files in dir
-1: (that's a ONE) only one entry per line. Change it to -1a if you want hidden files too
|: pipe output onto...
wc: "wordcount"
-l: count lines.

Trimming linux log files [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about a specific programming problem, a software algorithm, or software tools primarily used by programmers. If you believe the question would be on-topic on another Stack Exchange site, you can leave a comment to explain where the question may be able to be answered.
Closed 9 years ago.
Improve this question
It seems like a trivial issue, but I did not find a solution.
I have a number of log files in a php installation on Debian/Linux that tend to grow quite a bit and I would like to trim nightly to the last 500 lines or so.
How do I do it, possibly in shell and applying a command to *log?
For this, I would suggest to use logrotate with a configuration to your liking instead of programming your own script.
There might be a more elegant way to do this programmatically, but it is possible to use tail and a for-loop for this:
for file in *.log; do
tail -500 "$file" > "$file.tmp"
mv -- "$file.tmp" "$file"
done
If you want to save history of older files, you should check out logrotate.
Otherwise, this can be done trivially with the command line:
LOGS="/var/log"
MAX_LINES=500
find "$LOGS" -type f -name '*.log' -print0 | while read -d '' file; do
tmp=$(mktemp)
tail -n $MAX_LINES $file > $tmp
mv $tmp $file
done

Resources