Rename multiple files? [duplicate] - linux

This question already has answers here:
Rename multiple files based on pattern in Unix
(24 answers)
Closed 3 years ago.
I have several files with the extension *.php in different subfolders in the folder /root/Hello. I try to rename all .php files to .html but I want to keep the structure i.e. the path to the file should remain identical.
I found all files with the following command:
find /root/Hello -name "*.php"
But I don't know how I can rename all files with *.php to *.html and keep the structure I think I must use:
-exec
But I don't which argument I should use with -exec

use find:
find /path -depth -name "*.php" -exec sh -c 'mv "$1" "${1%.php}.html"' _ {} \;

Related

How to exclude some extension files when copy a folder [duplicate]

This question already has answers here:
Exclude files in a shell script that has a certain pattern
(2 answers)
Closed 3 months ago.
I need copy folderA to folder_B, but I want exclude all *.xml when copy.
How can I use a linux cmd to implement the requirement in ternimal?
I was asked to find a different way than cp -rf sourcedir/!(*.xml) dest_dir/
Suggesting to use find command:
cp $(find sourcedir -type f -not -name "*.xml") dest_dir
Suggesting to try and tune the find command:
find sourcedir -type f -not -name "*.xml"

Rename all files containing a specific string [duplicate]

This question already has answers here:
Bash command-line to rename wildcard
(2 answers)
Closed 7 months ago.
I want to rename files of the type file_name (1).extension to file_name.extension. How can i use rename to delete the " (1)" string ?
you can use the find [path_to_dir] -type f -name "*file_extension*" -exec sh -c 'x="{}"; mv "$x" "${x}_renamed"' \;
In my case, i find all .txt files in a folders, and rename it to filename_renamed.
find . -type f -name '*txt*' -exec sh -c 'x="{}"; mv "$x" "${x}_renamed"' \;

A script that deletes all the regular files (not the directories) with a .js extension that are in the current directory and its subfolders [duplicate]

This question already has answers here:
How to loop through a directory recursively to delete files with certain extensions
(16 answers)
Closed 2 years ago.
Write a script that deletes all the regular files (not the directories) with a .js extension that are present in the current directory and all its subfolders.
The answer should only contain one command after the shebang line, I've tried the following:
#!/bin/bash
rm -R *.js
… and:
#!/bin/bash
rm -f *.js
find . -name "*.js" -delete
Find all files in the current and child directories with the extension .js and delete the files.
The best way to achieve this remains the find command:
find . -type f -name '*.js' -exec rm -f {} \;
If however you want to stick to rm alone, it remains possible if you know exactly how many subdirectories lie under the place you're working in. For instance:
rm -f */*/*.js

Manipulating strings (file extensions) in bash using find [duplicate]

This question already has answers here:
How do I rename the extension for a bunch of files?
(28 answers)
Closed 6 years ago.
I'm having trouble manipulating strings in bash. I wish to re-write extensions.
I have the following 2 files in a Downloads directory
Example 001.mkv
Example 002.mkv
Using the script below I always get the same filenames returned without .mkv rewritten into .mp4.
find /Downloads -name \*.mkv -execdir echo $(file={}; echo ${file/mkv/mp4};) \;
I understand this isn't all you need to re-format a file but this script is part of a larger script that is passed to FFMPEG.
Here is the full command with FFMPEG.
find /Downloads -name \*.mkv -execdir ffmpeg -i {} -vcodec copy -acodec copy $(file={}; echo ${file/mkv/mp4};) \;
The exec and execdir are generally intended to actually execute command not to echo/print info about the files found (print/printf).
There are several ways to do this and here's one.
You could first try using the rename command that can use regex substitution for renaming files. This would require all the files to be renamed to be in the same folder /Downloads: (syntax may very according to the implementation of rename that ships with your distro)
ls *.mkv
a.mkv b.mkv
rename .mkv .mp4 *.mkv
ls *.mp4
a.mp4
b.mp4
Let's suppose that the mkv files are also present in subdirectories of /Downloads:
find . -type f -name "*.mkv"
./sundir/d.mkv
./sundir/c.mkv
./a.mkv
./b.mkv
find -type f -name "*.mkv" -exec rename .mkv .mp4 {} \;
find . -type f -name "*.mp4"
./sundir/c.mp4
./sundir/d.mp4
./b.mp4
./a.mp4
You can try using bash -c to execute your command
find /Downloads -name \*.mkv -execdir bash -c 'file={}; echo ${file/.mkv/.mp4}' \;

Want to find any reference in any file to a certain string in linux [duplicate]

This question already has answers here:
how to find files containing a string using egrep
(7 answers)
Closed 8 years ago.
I am trying to search All .PHP files or ALL .SH files for any reference that contains:
'into tbl_free_minutes_mar'
I have command line access to the server but the files may be scattered in different directories.
For all directories everywhere,
find / -type f \( -name '*.php' -o -name '*.sh' \) \
-exec fgrep 'into tbl_free_minutes_mar' {} \+
For fewer directories elsewhere, just give a list of paths instead of /. To just list the matching files, try fgrep -l. If your file names might not always match the wildcards in the -name conditions, maybe scan all files.
find / -type f \( -name \*.php -o -name \*.sh \) -exec grep 'into tbl_free_minutes_mar' {} /dev/null \;
Change find / ... to to something less all-encompassing if you know the general area that you want to look in, e.g. find /home ...
Provided /base/path is the path where you want to start looking this will get you a list of files:
find /base/path -type f -iregex '.*\.\(php\|sh\)$' -exec grep -l 'into tbl_free_minutes_mar' '{}' \;

Resources