Adding a line to multiple files in directory - linux

I have a line to be added to 3rd line of all files in directory. What's the commandline to do this operation
Lets say I want to add "color #c2451 " to 3rd line of files in Class directory

Try and use the find cmd piped into xargs and the sed cmd.
You'd have to cd into the directory first with the files.
find . -type f -name '*' | xargs sed -i "3i color #c2451"
Add text to file at certain line in Linux
Change multiple files

Related

Add file name to top of each column (tab separated) for multiple files

I have many files in a single directory, the first few are named:
1A_T
1B_T
2B
3_6A_T
Each file has three columns of data that are tab delimited.
I need to add the name of the file to the top of each column in the file, and do this for all files in the directory. Keeping everything tab delimited is crucial.
Simple suggestions? I've tried several approaches (I'm new to this) from the internet but can't get it to work properly.
Thanks for your help
You can use the following command:
for f in `find . -type f`; do echo $f; sed -i.bak "1 i $f\t$f\t$f" $f; done
just in case test it in a different directory before doing it on your working directory.
Explanations:
find . -type f will get all the files from your directory (and its sub dir) (run this command from the directory where you want to proceed or change . accordingly with the PATH to your dir)
for f in will iterate on all your files you could also use xargs command
sed -i.bak "1 i $f\t$f\t$f" $f; will take a backup of your file then edit it and add before the first line of your file the filename\tfilename\tfilename
TESTED:
REMARK:
beware of spaces in the filenames!
Last but not least, I have taken both of your points in the comment and you can use the following command in order to not have the ./ before the file name on the first line and to process files only in the working directory.
for f in `find . -maxdepth 1 -type f`; do echo `basename $f`; sed -i.bak "1 i `basename $f`\t`basename $f`\t`basename $f`" $f; done

Find a zip file, print path and zip contents

I have a series of numbered sub-directories that may or may not contain zip files, and within those zip files are some single-line .txt files I need. Is it possible to use a combination of find and unzip -p to list the file path and the single line contents on the same output line? I'd like to save the results to a .txt and import it into excel to work with.
From the main directory I can successfully find and output the single line:
find . -name 'file.zip' -exec unzip -p {} file.txt \;
How can I prefix the find output (i.e. the file path) to the output of this unzip command? Ideally, I'd like each line of the text file to resemble:
./path/to/file1.zip "Single line of file1.txt file"
./path/to/file2.zip "Single line of file2.txt file"
and so on. Can anyone provide some suggestions? I'm not very experienced with linux command line beyond simple commands.
Thank you.
Put all the code you want to execute into a shell script, then use the exec feature to call the shell script, i.e.
cat finder.bash
#!/bin/bash
printf "$# : " # prints just the /path/to/file/file.zip
unzip -p "$#" file.txt
For now, get that to work, you can make it generic to pass others besides file.txt later.
Make the script executable
chmod 755 finder.bash
Call it from find. i.e.
find . -name 'file.zip' -exec /path/to/finder.bash {} \;
(I don't have an easy way to test this, so reply in comments with error msgs).

I would like to add a line in a file on a linux shell

I am new to the Linux Command Line and truth be told, I am a little intimidated be all the commands. I would like to add a line between find. What commands are there for? There is a lot out there about files but nothing about the content.
cd /applbeh/cr/acr/xfb/send/
find Mt940 -type f -iname '*MT940*' | xargs -r rm -v
find Mt940 -type f -iname '*OVK*' | xargs -r rm -v
Thanks
A oneliner to add a line to a file is:
echo 'line' >> file
With line (without the quotes), the content of the line you wish to add and file the name of the file you wish to write to.
>> is an IO redirection that means append, it means you add the given output (echo 'line') thus line to the file.

Change all file name and extension in directory in linux or windows

I have thousands of file with file extensions like this
3_bedroom_villas_in_chennai.html__201308050010_
3_bedroom_villas_in_chennai.html__201308080012_
3_bedroom_villas_in_chennai.html__201308100012_
3_bedroom_villas_in_chennai.html__201308110034_ and so on.....
inside a directory. I wanna change all these into the following
3_bedroom_villas_in_chennai__201308050010_.html
3_bedroom_villas_in_chennai__201308080012_.html
3_bedroom_villas_in_chennai__201308100012_.html
3_bedroom_villas_in_chennai__201308110034_.html
When I tried doing it in windows using the following command
ren *.* *.html
I got the following
A duplicate file name exists, or the file
cannot be found.
A duplicate file name exists, or the file
cannot be found.
A duplicate file name exists, or the file
cannot be found.
A duplicate file name exists, or the file
cannot be found. and so on...
Because I know that it will try to change everything into a single file name like
3_bedroom_villas_in_chennai.html and so on...
any ways to do this either on windows or linux ??
In Linux:
ls | xargs -I % mv % %.html
ls command output is piped to xargs, and xargs replaces all % (after mv) with the input from ls
Also, if you want recursively go through all sub-directories, you might want to use:
find . -type f | xargs -I % mv % %.html
And in Windows:
for /r %x in (*) do ren "%x" *.txt
using renamer:
$ renamer --regex --find '(.*).html(.*)' --replace '$1$2.html' *
Works on Windows, Mac and Linux.

copy the content of many file to one file

I have many files and I want to copy the content of these files in one file.
how to do that using linux command.
Exemple :
folder1\text1.txt
folder1\text2.txt
folder1\text3.txt
folder1\text5.txt
folder1\text4.txt
folder1\text6.txt
etc
copy the contents of all file into folder1\text.txt
thank
You can do
cat folder1/text*.txt > folder1/text.txt
It will get all files matching folder1/text*.txt pattern and put its content in folder1/text.txt.
Note I used folder/text.txt, that is, forward slash. Backslash is not used in *NIX.
you can use
find folder1 -name "text.*.txt" -type f -exec cat {} >> folder1/text.txt
when in folder type in command line
cat *.txt >> text.txt

Resources