I want to delete a line with string 'Generation' in all the files under a folder named KCG. I tried the following in Windows OS command prompt:
sed -i '/Generation/d' file
But got the following error message:
sed: can't read file: No such file or directory
Next I tried:
sed -i '/Generation/d' airport_related_altitudes_derived_data.c
This worked, but I do not want to enter the filenames of all the files in the folder each time. Is there a command to recursively look for the string in all the files under the folder?
Try this:
find . -type f -exec sed -i '/Generation/d' {} \;
That will recursively find all files ("-type f") under the current directory (".") and call "sed -i '/Generation/d'" on each file ("{}").
A windows way to enumerate files of a pattern/file type in current folder.
#Echo off
For %%A in (*.c) Do sed -i '/Generation/d' %%A
Recursive from a start path
For /r "X:\start\path" %%A in (*.c) do sed -i '/Generation/d' %%A
A pure batch way to drop lines containing words from a file is using find /v or findstr /v but requires a different output file name, no inplace editing.
#Echo off
for %%A in (*.c) do findstr /vi "Generation" "%%~A" >"%%~dpnA_new%%~xA"
what creates new files with a appended _new before the extension.
It's of course possible to rename the old to .bak and write the changed version to the original name.
Related
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
I am trying to rename the files and directories using a text file separated by space.
The text file looks like this:
dir1-1 dir1_1
dir2-1 dir223_1
My command is as follows:
xargs -r -a files.txt -L1 mv
This command can rename only folders from dir1-1 to dir1_1 and dir2-1to dir223_1so on but it doesn't rename the files in the subdirectories. The files in the corresponding directories also have these prefix of these directories.
Looking forward for the assistance.
Assuming you don't have special characters(space of tab...) in your file/dir names,
try
perl_script=$(
echo 'chop($_); $orig=$_;'
while read -r src tgt; do
echo 'if (s{(.*)/'"$src"'([^/]*)}{$1/'"$tgt"'\2}) { print "$orig $_\n";next;}'
done < files.txt)
find . -depth | perl -ne "$perl_script" | xargs -r -L1 echo mv
Remove echo once you see it does what you wanted.
I have 1000 .mp4 files in a folder in linux and the name of those files are like this:
filename_mywebsite.com_.mp4
I need to rename all files with one command
so how i can change
_mywebsite.com_ to _mywebsite2.com_
in all files
so all my files will be
filename_mywebsite2.com_.mp4
Try renaming it as:
rename 's/.com_/2.com_/' filename_mywebsite.com_.mp4
run below rename command in local directory.
rename 's/mywebsite/mywebsite2/' *.mp4
A piece of code to rename all the files with the same prefix and appends incremental value
declare -i x=1
for f in $(find -type f); do
mv -v $f ${f%/*}/change_me_$x ;
x=$x+1;
done
I am on a Linux system and I am trying to rename all .jpg files in many subdirectories to sequential filenames, so all the jpeg files in each subdirectory are renamed 0001.jpg, 0002.jpg, etc. I have a 'rename' command that works in a single directory:
rename -n 's/.*/sprintf("%04d",$::iter++ +1).".jpg"/e' *.jpg
I am trying to use it like this:
for i in ls -D; do rename -n 's/.*/sprintf("%04d",$::iter++ +1).".jpg"/e' *.jpg; done
but for output I get this:
*.jpg renamed as 0001.jpg
for each subdirectory. What am I doing wrong?
You need to put the command in backticks (or use the $( ... ) bash syntax) in order
to iterate over its output. Also use the $i variable together with the *.jpg file
name pattern, e.g.
for i in `ls -D`
do
rename -n 's/.*/sprintf("%04d",$::iter++ +1).".jpg"/e' $i/*.jpg
done
however, for this scenario you want to iterate over all the subdirectories, and you are
better of using the find command:
for i in `find . -type d`; do rename ...
It seems to me you've forgot to change a current working directory so it should looks like
for i in *; do
[ -d "$i" ] || continue
pushd "$i"
# rename is here
popd
done
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.