Find file and cd into it - bash

I am attempting to find multiple files, then quit after the first match and then cd into this match, I have attempted:
find `pwd` -iname 'tensorflow' -type d -exec echo {} \; -quit | xargs -I{} cd {}
However, this does nothing and it won't enter into that directory.

There is no /usr/bin/cd, it's not an executable. You have to run it in current shell, not in subshell as part of pipeline.
Do not use backticks. Prefer $(...).
find pwd? Just find ., you are already in pwd.
-exec echo {} \;? Just -print it.
dir=$(find . -iname 'tensorflow' -type d -print -quit)
cd "$dir"

Related

Copy all *.txt to *.new.txt but ignore existing *.new.txt files

I need script for this:
Copy all *.txt to *.new.txt
When run this script, ignore all *.new.txt files.
What I try:
find ~/folder -type f -name "*.txt" ! -name "*.new.txt"
how to send this output to
cp STDIN *.new.txt
Use -exec to execute a subshell where you copy the file to the modified filename.
find ~/folder -type f -name "*.txt" ! -name "*.new.txt" -exec bash -c 'cp "$1" "${1/.txt/.new.txt}"' {} {} \;

GNU find following symbolic links and name pattern fails to execute remove (rm)

I tried to remove files which might be located in symlinked directories. I recognized for certain basenames, that find fails with the error No such file or directory.
Here is what I tried. Am I doing something wrong? Thanks!
touch a_b.c.d
touch a_b.d
find -L . -type f -name '*.c.d' -exec bash -c 'rm $(basename {} .c.d).d' \;
This fails, too
touch a_b.c.d
touch a_b.d
find -L . -type f -name '*.c.d' -exec rm a_b.d \;
But this works
touch a_x_b.c.d
touch a_x_b.d
find -L . -type f -name '*.c.d' -exec bash -c 'rm $(basename {} .c.d).d' \;
or this, respectively
touch a_x_b.c.d
touch a_x_b.d
find -L . -type f -name '*.c.d' -exec rm a_x_b.d \;

find exec and strip extension from filenames

Any idea why this command is not working? btw, I'm trying to strip out the extensions of all csv files in current directory.
find -type f -iname "*.csv" -exec mv {} $(basename {} ".csv") \;
Tried many variants including the parameter expansions, xargs ... Even then all went futile.
This should do:
find ./ -type f -iname "*.csv" -exec sh -c 'mv {} $(basename {} .csv)' \;
find is able to substitute {} with its findings since the quotes prevent executing the subshell until find is done. Then it executes the -exec part.
The problem why yours is not working is that $(basename {} ".csv") is executed in a subshell (-> $()) and evaluated beforehand. If we look at the command execution step-by-step you will see what happens:
find -type f -iname "*.csv" -exec mv {} $(basename {} ".csv") \; - your command
find -type f -iname "*.csv" -exec mv {} {} \; - subshell gets evaluated ($(basename {} ".csv") returns {} since it interprets {} as a literal)
find -type f -iname "*.csv" -exec mv {} {} \; - as you see now: move does actually nothing
First, take care that you have no subdirectories; find, without extra arguments, will automatically recur into any directory below.
Simple approach: if you have a small enough number of files, just use the glob (*) operator, and take advantage of rename:
$ rename 's/.csv$//' *.csv
If you have too many files, use find, and perhaps xargs:
$ find . -maxdepth 1 -type f -name "*.csv" | xargs rename 's/.csv$//'
If you want to be really safe, tell find and xargs to delimit with null-bytes, so that you don't have weird filenames (e.g., with spaces or newlines) mess up the process:
$ find . -maxdepth 1 -type f -name "*.csv" -print0 | xargs -0 rename 's/.csv$//'

I am getting an error "arg list too long" in unix

i am using the following command and getting an error "arg list too long".Help needed.
find ./* \
-prune \
-name "*.dat" \
-type f \
-cmin +60 \
-exec basename {} \;
Here is the fix
find . -prune -name "*.dat" -type f -cmin +60 |xargs -i basename {} \;
To only find files in the current directory, use -maxdepth 1.
find . -maxdepth 1 -name '*.dat' -type f -cmin +60 -exec basename {} \;
In all *nix systems the shell has a maximum length of arguments that can be passed to a command. This is measured after the shell has expanded filenames passed as arguments on the command line.
The syntax of find is find location_to_find_from arguments..... so when you are running this command the shell will expand your ./* to a list of all files in the current directory. This will expand your find command line to find file1 file2 file3 etc etc This is probably not want you want as the find is recursive anyway. I expect that you are running this command in a large directory and blowing your command length limit.
Try running the command as follows
find . -name "*.dat" -type f -cmin +60 -exec basename {} \;
This will prevent the filename expansion that is probably causing your issue.
Without find, and only checking the current directory
now=$(date +%s)
for file in *.dat; do
if (( $now - $(stat -c %Y "$file") > 3600 )); then
echo "$file"
fi
done
This works on my GNU system. You may need to alter the date and stat formats for different OS's
If you have to show only .dat filename in the ./ tree. Execute it without -prune option, and use just path:
find ./ -name "*.dat" -type f -cmin +60 -exec basename {} \;
To find all the .dat files which are older than 60 minutes in the present directory only do as follows:
find . -iregex "./[^/]+\.dat" -type f -cmin +60 -exec basename {} \;
And if you have croppen (for example aix) version of find tool do as follows:
find . -name "*.dat" -type f -cmin +60 | grep "^./[^/]\+dat" | sed "s/^.\///"

Using find(1) in command substitution and quote filenames with blanks

I'd like to use find inside a command substitution, where the returned filenames contain whitespace. What option do I need so it correctly quotes the filenames? I tried -print0, but it will not work in the shell itself.
example:
command $(find . -type f) some other params
I also tried with -exec echo "{}" \;, but that was of no help either.
If I use set -x to display shell expansion and the actual command which is executed I get:
$ command `find -type f -printf \"%p\"\ ` some other params
++ find -type f -printf '"%p" '
+ command '"./file_with' 'blanks"' '"./another' 'file"' some other params
Where are the single quotation marks coming from and why are they applied to each "word"?
Put the find result in an array, and run command "${array[#]}" some other params.
Maybe the printf action is more amenable to being contained in a substitution (GNU find only, though):
command $(find . -type f -printf \"%P\"\ ) some other params
The %P placeholder is the filename minus the argument to find, so in cases other than find ., you'd probably want %p instead.
find /what/ever -name "what ever" -exec echo "\{\}" \;
works here (Ubuntu 10.04 default gterm with bash)
Just tried
find /bin -name ls -exec \{\} -lah \;
`find /bin -name ls -exec echo \{\} \;` -lah
MYCMD=`find /bin -name ls -exec echo \{\} \;` && $MYCMD -lah
MYCMD=$(`find /bin -name ls -exec echo \{\} \;` -lah) && echo $MYCMD
MYCMD=$(`find /bin -name ls` -lah) && echo $MYCMD
all work as expected

Resources