A
A
Anatoly2018-06-01 06:04:14
linux
Anatoly, 2018-06-01 06:04:14

How to use the awk utility with the find utility when run via -exec?

I can't figure out how to use awk with find if run via -exec.
Example:
# command finds all files in a folder and displays only their names
find ~/myfiles/ | awk -F"/" '{print $NF}'
works!
but if I write this:
find ~/myfiles/ -exec awk -F"/" '{print $NF}' \;
then nothing works
like this, it doesn't work either:
find ~/myfiles/ -exec echo {} \; | awk -F"/" '{print $NF}' \;
how to be?

Answer the question

In order to leave comments, you need to log in

3 answer(s)
A
Anatoly, 2018-06-01
@Tolly

find ~/myfiles -exec DOING SOMETHING \; -exec echo {} \; | sed 's|.*/\([^/]*\)$|\1|'
find ~/myfiles -exec DOING SOMETHING \; -exec basename {} \;
find ~/myfiles -exec DOING SOMETHING \; -exec echo "File -" `basename {}` \;
something does not plow the last line, only through the loop:
for i in `find ~/myfiles`
do
echo ">> " `basename $i`
done

E
Eugene, 2018-06-01
@immaculate

It's not clear what you want to achieve. If you run awk with a file as an argument, then this command works:
Note that /awk arguments do not need to be escaped, although this is not an error - this character has no special meaning.

H
Harbid Abu Marhamedoff, 2021-04-20
@harbid

This is all great, but it doesn't work properly because for breaks the filename into separate words if it contains spaces.
I now have a need to make a script that would run through a directory with subdirectories and form a general list of directories and files like:
==================
/home/user 2021-01- 05 16384
/home/user/tmp 2021-02-11 16384
/home/user/tmp/1 2 3.txt 2021-04-20 2
==================
and I'm sick of it :( Here, degenerated:
for i in `find /home/user/*`;do ls -l --time-style=+%Y-%m-%d $i|awk '{print $7" "$6" "$5}';done
does what it's supposed to do, but breaks up names with spaces, making the result a mess :( What to do?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question