unix bash command - bash

How to write a bash command that finds all files in the current directory that contain the word “foo”, regardless of case?

If you want "foo" to be the checked against the contests of the files in ., do this:
grep . -rsni -e "foo"
for more options (-I, -T, ...) see man grep.

Assuming you want to search inside the files (not the filenames)
If you only want the current directory to be searched (not the tree)
grep * -nsie "foo"
if you want to scan the entire tree (from the current directory)
grep . -nsrie "foo"

shopt -s nullglob
shopt -s nocaseglob
for file in *foo*
...
...
..

Try:
echo *foo*
will print file/dir names matching *foo* in the current directory, which happens to match any name containing 'foo'.

I've always used this little shell command:
gfind () { if [ $# -lt 2 ]; then files="*"; search="${1}"; else files="${1}"; search="${2}"; fi; find . -name "$files" -a ! -wholename '*/.*' -exec grep -Hin ${3} "$search" {} \; ; }
you call it by either gfind '*php' 'search string' or if you want to search all files gfind 'search string'

find . -type f | grep -i "foo"

Related

Unix shell script to find a file and replace name by pattern

I have a folder called /input/temp. Inside the folder I have lot of files. I need to find the file of pattern Article_????_test_?????????.txt and replace by format below.
Article_????_?????????.txt
Below is the code I tried and which doesn't work:
echo "Please provide the file name Corresponding to DC..."
read file
ls $HOME/*.txt | grep $file
if [ $? -eq 0 ]
find . $file '*_Test_*' -exec bash -c 'mv $0 ${0/_Test/ }' {} \;
if [ $? -eq 0 ]
find . $file -name "*.txt" -exec bash -c "mv {} \`echo {} | sed -e 's/[_TEST_]/_/g'\`" \;
then
I Got below error:
find: 0652-083 Cannot execute bash:: A file or directory in the path name does not exist.
find: 0652-083 Cannot execute bash:: A file or directory in the path name does not exist.
bash can't be executed on my platform.
Unless the file name is a regular expression you can use if [ -e "$file" ] instead of ls + grep + test.
When you do find . $file '*_Test_*' the last three parameters are actually taken as files or directories to search underneath! It will return
all files in the current directory,
all files in the directory $file or the path $file if it's not a directory, and
all files in any directories matching *_Test_* or their paths if they are not directories.
There's no need for bash - you can run mv directly in -exec. This is just extra complexity for no gain.
Use $(command) instead of command for much easier quote handling. Each $() has a separate quoting context, so you can do for example echo "$(command "$(c2 "argument with spaces")")".
According to the link here:
This should work
ls -1 Article_????test?????????.txt|awk '{old=$0;gsub(/test/,"_",$0);system("mv \""old"\" "$0)}'
Also try the 'rename' command.
rename 's/_test_/_/' *.txt
You can fine tune the regular expression...
Update from your code:
cd $HOME
find . -name '*_Test_*' |while read line
do
echo mv ${line) ${line/_Test/}
done
If you need search the pattern Article_????test?????????.txt, try this
cd $HOME
find . -name 'Article_????_test_?????????.txt' |while read line
do
echo mv ${line) ${line/_Test/}
done

Find path in bash on insensitive manner

Suppose a path like
/home/albfan/Projects/InSaNEWEBproJECT
Despite of the fact to not use such that names. Is there a way to check for a path in an insensitive manner?
I came across to this solution, but I would like to find a builtin or gnu program, if it is possible.
function searchPathInsensitive {
# Replace bar with comma (not valid directory character allowing parse dirs with spaces)
#also remove first / if exist (if not this create a first empty element
ORG="$1"
if [ "${ORG:0:1}" = "/" ]
then
ORG="${ORG:1}"
else
ORG="${PWD:1}/$ORG"
fi
OLDIFS=$IF
IFS=,
for dir in ${ORG//\//,}
do
if [ -z $DIR ]
then
DIR="/$dir"
else
TMP_DIR="$DIR/$dir"
DIR=$(/usr/bin/find $DIR -maxdepth 1 -ipath $TMP_DIR -print -quit)
if [ -z $DIR ]
then
# If some of the path does not exist just copy the element
# exit 1
DIR="$TMP_DIR"
fi
fi
done
IFS=$OLDIFS
echo "$DIR"
}
to use it just do:
(searching on my home)
$ searchPathInsensitive projects/insanewebproject
/home/albfan/Projects/InSaNEWEBproJECT
(inside a project)
$ searchPathInsensitive src/main/java/org/package/webprotocolhttpwrapper.java
/home/albfan/Projects/InSaNEWEBproJECT/src/main/java/org/package/WebProtocolHTTPWrapper.java
$ searchPathInsensitive src/main/resources/logout.png
/home/albfan/Projects/InSaNEWEBproJECT/src/main/resources/LogOut.PNG
I guess the solution is related in any way with find -ipath as all I do with the function is search only for next element in path given on insensitive manner
My fault! I guess I tried
find -ipath 'projects/insanewebproject'
but the trick here is that I must use
find -ipath './projects/insanewebproject'
That ./ does the change. Thanks!.
man says -path is more portable than -wholename
if you expect only one result, you can add | head -n1, cause that way head kill pipe when it fills its buffer, which is only one line length
find -ipath './projects/insanewebproject'| head -n1
The simplest solution:
$ find . | grep -qi /path/to/something[^/]*$
But if you have some additional conditions that must be checked for matched file, you can run grep inside find:
$ find . -exec sh -c 'echo {} | grep -qi /path/to/something' \; -print
Here you will get all files that are in the directory. If you want to get only the directory's name:
$ find . -exec sh -c 'echo {} | grep -qi /path/to/something[^/]*$' \; -print
Example of usage:
$ mkdir -p Projects/InSaNEWEBproJECT/src/main/resources/
$ find . -exec sh -c 'echo {} | grep -qi /projects/insanewebproject[^/]*$' \; -print
./Projects/InSaNEWEBproJECT

Rename file names in current directory and all subdirectories

I have 4 files with the following names in different directories and subdirectories
tag0.txt, tag1.txt, tag2.txt and tag3.txt
and wish to rename them as tag0a.txt, tag1a.txt ,tag2a.txt and tag3a.txt in all directories and subdirectories.
Could anyone help me out using a shell script?
Cheers
$ shopt -s globstar
$ rename -n 's/\.txt$/a\.txt/' **/*.txt
foo/bar/tag2.txt renamed as foo/bar/tag2a.txt
foo/tag1.txt renamed as foo/tag1a.txt
tag0.txt renamed as tag0a.txt
Remove -n to rename after checking the result - It is the "dry run" option.
This can of course be done with find:
find . -name 'tag?.txt' -type f -exec bash -c 'mv "$1" ${1%.*}a.${1##*.}' -- {} \;
Here is a posix shell script (checked with dash):
visitDir() {
local file
for file in "$1"/*; do
if [ -d "$file" ]; then
visitDir "$file";
else
if [ -f "$file" ] && echo "$file"|grep -q '^.*/tag[0-3]\.txt$'; then
newfile=$(echo $file | sed 's/\.txt/a.txt/')
echo mv "$file" "$newfile"
fi
fi
done
}
visitDir .
If you can use bashisms, just replace the inner IF with:
if [[ -f "$file" && "$file" =~ ^.*/tag[0-3]\.txt$ ]]; then
echo mv "$file" "${file/.txt/a.txt}"
fi
First check that the result is what you expected, then possibly remove the "echo" in front of the mv command.
Using the Perl script version of rename that may be on your system:
find . -name 'tag?.txt' -exec rename 's/\.txt$/a$&/' {} \;
Using the binary executable version of rename:
find . -name 'tag?.txt' -exec rename .txt a.txt {} \;
which changes the first occurrence of ".txt". Since the file names are constrained by the -name argument, that won't be a problem.
Is this good enough?
jcomeau#intrepid:/tmp$ find . -name tag?.txt
./a/tag0.txt
./b/tagb.txt
./c/tag1.txt
./c/d/tag3.txt
jcomeau#intrepid:/tmp$ for txtfile in $(find . -name 'tag?.txt'); do \
mv $txtfile ${txtfile%%.txt}a.txt; done
jcomeau#intrepid:/tmp$ find . -name tag*.txt
./a/tag0a.txt
./b/tagba.txt
./c/d/tag3a.txt
./c/tag1a.txt
Don't actually put the backslash into the command, and if you do, expect a '>' prompt on the next line. I didn't put that into the output to avoid confusion, but I didn't want anybody to have to scroll either.

How do I remove a specific extension from files recursively using a bash script

I'm trying to find a bash script that will recursively look for files with a .bx extension, and remove this extension. The filenames are in no particular format (some are hidden files with "." prefix, some have spaces in the name, etc.), and not all files have this extension.
I'm not sure how to find each file with the .bx extension (in and below my cwd) and remove it. Thanks for the help!
find . -name '*.bx' -type f | while read NAME ; do mv "${NAME}" "${NAME%.bx}" ; done
find -name "*.bx" -print0 | xargs -0 rename 's/\.bx//'
Bash 4+
shopt -s globstar
shopt -s nullglob
shopt -s dotglob
for file in **/*.bx
do
mv "$file" "${file%.bx}"
done
Assuming you are in the folder from where you want to do this
find . -name "*.bx" -print0 | xargs -0 rename .bx ""
for blah in *.bx ; do mv ${blah} ${blah%%.bx}
Here is another version which does the following:
Finds out files based on $old_ext variable (right now set to .bx) in and below cwd, stores them in $files
Replaces those files' extension to nothing (or something new depending on $new_ext variable, currently set to .xyz)
The script uses dirname and basename to find out file-path and file-name respectively.
#!/bin/bash
old_ext=".bx"
new_ext=".xyz"
files=$(find ./ -name "*${old_ext}")
for file in $files
do
file_name=$(basename $file $old_ext)
file_path=$(dirname $file)
new_file=${file_path}/${file_name}${new_ext}
#echo "$file --> $new_file"
mv "$file" "$new_file"
done
Extra: How to remove any extension from filenames
find -maxdepth 1 -type f | sed 's/.\///g'| grep -E [.] | while read file; do mv $file ${file%.*}; done
will cut starting from last dot, i.e. pet.cat.dog ---> pet.cat
find -maxdepth 1 -type f | sed 's/.\///g'| grep -E [.] | while read file; do mv $file ${file%%.*}; done
will cut starting from first dot, i.e. pet.cat.dog ---> pet
"-maxdepth 1" limits operation to current directory, "-type f" is used to select files only. Sed & grep combination is used to pick only filenames with dot. Number of percent signs in "mv" command will define actual cut point.

Bash rename extension recursive

I know there are a lot of things like this around, but either they don't work recursively or they are huge.
This is what I got:
find . -name "*.so" -exec mv {} `echo {} | sed s/.so/.dylib/` \;
When I just run the find part it gives me a list of files. When I run the sed part it replaces any .so with .dylib. When I run them together they don't work.
I replaced mv with echo to see what happened:
./AI/Interfaces/C/0.1/libAIInterface.so ./AI/Interfaces/C/0.1/libAIInterface.so
Nothing is replaced at all!
What is wrong?
This will do everything correctly:
find -L . -type f -name "*.so" -print0 | while IFS= read -r -d '' FNAME; do
mv -- "$FNAME" "${FNAME%.so}.dylib"
done
By correctly, we mean:
1) It will rename just the file extension (due to use of ${FNAME%.so}.dylib). All the other solutions using ${X/.so/.dylib} are incorrect as they wrongly rename the first occurrence of .so in the filename (e.g. x.so.so is renamed to x.dylib.so, or worse, ./libraries/libTemp.so-1.9.3/libTemp.so is renamed to ./libraries/libTemp.dylib-1.9.3/libTemp.so - an error).
2) It will handle spaces and any other special characters in filenames (except double quotes).
3) It will not change directories or other special files.
4) It will follow symbolic links into subdirectories and links to target files and rename the target file, not the link itself (the default behaviour of find is to process the symbolic link itself, not the file pointed to by the link).
for X in `find . -name "*.so"`
do
mv $X ${X/.so/.dylib}
done
A bash script to rename file extensions generally
#/bin/bash
find -L . -type f -name '*.'$1 -print0 | while IFS= read -r -d '' file; do
echo "renaming $file to $(basename ${file%.$1}.$2)";
mv -- "$file" "${file%.$1}.$2";
done
Credits to aps2012.
Usage
Create a file e.g. called ext-rename (no extension, so you can run it like a command) in e.g. /usr/bin (make sure /usr/bin is added to your $PATH)
run ext-rename [ext1] [ext2] anywhere in terminal, where [ext1] is renaming from and [ext2] is renaming to. An example use would be: ext-rename so dylib, which will rename any file with extension .so to same name but with extension .dylib.
What is wrong is that
echo {} | sed s/.so/.dylib/
is only executed once, before the find is launched, sed is given {} on its input, which doesn't match /.so/ and is left unchanged, so your resulting command line is
find . -name "*.so" -exec mv {} {}
if you have Bash 4
#!/bin/bash
shopt -s globstar
shopt -s nullglob
for file in /path/**/*.so
do
echo mv "$file" "${file/%.so}.dylib"
done
He needs recursion:
#!/bin/bash
function walk_tree {
local directory="$1"
local i
for i in "$directory"/*;
do
if [ "$i" = . -o "$i" = .. ]; then
continue
elif [ -d "$i" ]; then
walk_tree "$i"
elif [ "${i##*.}" = "so" ]; then
echo mv $i ${i%.*}.dylib
else
continue
fi
done
}
walk_tree "."

Resources