Linux/Cygwin recursively copy file change extension
find . -name "*.js" -exec bash -c 'name="{}"; cp "$name" "${name%.js}.ts"' \;
Using find
, you can execute a command directly on a file that you've found, by using the -exec
option; you don't need to pipe it through xargs. It takes the command name followed by arguments to the command, followed by a single argument ;
, which you have to escape to avoid the shell interpreting it. find
will replace any occurrence of {}
in the command name or arguments with the file found.
In order call a command with the appropriate ending substituted, there are multiple approaches you can take, but a simple one is to use Bash's parameter expansion. You need to define a shell parameter that contains the name (in this case, I creatively chose name={}
), and then you can use parameter expansion on it. ${variable%suffix}
strips off suffix
from the value of $variable
; I then add on .ts
to the end, and have the name I'm looking for.
Recursively change file extensions in Bash
Use:
find . -name "*.t1" -exec bash -c 'mv "$1" "${1%.t1}".t2' - '{}' +
If you have rename
available then use one of these:
find . -name '*.t1' -exec rename .t1 .t2 {} +
find . -name "*.t1" -exec rename 's/\.t1$/.t2/' '{}' +
How do I rename the extension for a bunch of files?
For an better solution (with only bash functionality, as opposed to external calls), see one of the other answers.
The following would do and does not require the system to have the rename
program (although you would most often have this on a system):
for file in *.html; do
mv "$file" "$(basename "$file" .html).txt"
done
EDIT: As pointed out in the comments, this does not work for filenames with spaces in them without proper quoting (now added above). When working purely on your own files that you know do not have spaces in the filenames this will work but whenever you write something that may be reused at a later time, do not skip proper quoting.
How can you recursively copy all of the *.foo files in src to target using cp and/or find?
If you want to use find / cp then the following should do the trick:
find -f -name *.jsp -exec cp --parents {} /dest/path \;
but rsync is probably the better tool.
Find multiple files and rename them in Linux
You can use find
to find all matching files recursively:
$ find . -iname "*dbg*" -exec rename _dbg.txt .txt '{}' \;
EDIT: what the '{}'
and \;
are?
The -exec
argument makes find execute rename
for every matching file found. '{}'
will be replaced with the path name of the file. The last token, \;
is there only to mark the end of the exec expression.
All that is described nicely in the man page for find:
-exec utility [argument ...] ;
True if the program named utility returns a zero value as its
exit status. Optional arguments may be passed to the utility.
The expression must be terminated by a semicolon (``;''). If you
invoke find from a shell you may need to quote the semicolon if
the shell would otherwise treat it as a control operator. If the
string ``{}'' appears anywhere in the utility name or the argu-
ments it is replaced by the pathname of the current file.
Utility will be executed from the directory from which find was
executed. Utility and arguments are not subject to the further
expansion of shell patterns and constructs.
How can I remove the extension of a filename in a shell script?
You should be using the command substitution syntax $(command)
when you want to execute a command in script/command.
So your line would be
name=$(echo "$filename" | cut -f 1 -d '.')
Code explanation:
echo
get the value of the variable$filename
and send it to standard output- We then grab the output and pipe it to the
cut
command - The
cut
will use the . as delimiter (also known as separator) for cutting the string into segments and by-f
we select which segment we want to have in output - Then the
$()
command substitution will get the output and return its value - The returned value will be assigned to the variable named
name
Note that this gives the portion of the variable up to the first period .
:
$ filename=hello.world
$ echo "$filename" | cut -f 1 -d '.'
hello
$ filename=hello.hello.hello
$ echo "$filename" | cut -f 1 -d '.'
hello
$ filename=hello
$ echo "$filename" | cut -f 1 -d '.'
hello
How can I grep recursively, but only in files with certain extensions?
Just use the --include
parameter, like this:
grep -inr --include \*.h --include \*.cpp CP_Image ~/path[12345] | mailx -s GREP email@domain.example
That should do what you want.
To take the explanation from HoldOffHunger's answer below:
grep
: command-r
: recursively-i
: ignore-case-n
: each output line is preceded by its relative line number in the file--include \*.cpp
: all *.cpp: C++ files (escape with \ just in case you have a directory with asterisks in the filenames)./
: Start at current directory.
How can I associate .sh files with Cygwin?
Ok, I've found something that works. Associating a batch file as Vladimir suggested didn't work, but the bash arguments were key.
Short and sweet: associate with this command: "C:\cygwin\bin\bash.exe" -li "%1" %*
Long version if you don't know how:
- In Explorer, go to Tools/Folder Options/File Types.
- I already had an SH entry for Bash Script. If you don't have one, click New and enter "SH" to create one.
- With the SH extension selected, click Advanced.
- Choose the "open" action and click edit (or create the action).
- This is the command to use: "C:\cygwin\bin\bash.exe" -li "%1" %*. Note that without the -li, it was returing "command not found" on my scripts.
You may also want to add SH to your PATHEXT environment variable:
WinKey+Pause / Advanced / Environment Variables / System Variables / PATHEXT
Thanks for your help, guys!
How to replace a string in multiple files in linux command line
cd /path/to/your/folder
sed -i 's/foo/bar/g' *
Occurrences of "foo" will be replaced with "bar".
On BSD systems like macOS, you need to provide a backup extension like -i '.bak'
or else "risk corruption or partial content" per the manpage.
cd /path/to/your/folder
sed -i '.bak' 's/foo/bar/g' *
Related Topics
Automated Installation of R-Studio Using Shell Script
Run 'Perf Stat' on The Output of 'Perf Record'
Linux Kernel Hardware Break Points
Why Isn't Git Bash Transforming The Path to *Nix Notation for My Python Installation
Mqtt Socket Error on Client <Unknown>
Why Does 'Ping' Not Timeout in Linux
Linux - Change The Hostname in The Cli
Ffmpeg Img to Video = Could Find No File with Path
Linux + Verify If File Is Text or Binary
Why Does The -R Option (Relocatable) Make Ld Not Find Any Libraries
Are Debug Symbols Loaded into Memory on Linux
How to Have Postgresql Not Collapse Punctuation and Spaces When Collating Using a Language
Avoid Daemon Running in Dedicated CPU Cores
Execute External Program with Trigger in Postgres 9.4
How to Send Email from The Command Line
How to Find The Version of a Compiled Kernel Module
How to Prevent Git from Committing Two Files with Names Differing Only in Case