how to add text to an output redirected to a file - linux

I don't know if I'm wording it correctly, but I'm counting file types and outputting the results into a file, and instead of there just being numbers, I'm trying to identify what each number is. Sooo basically right now I have:
$ find . -type f -iname *.jpg* | wc -l > Test.md
$ find . -type f -iname *.png* | wc -l >> Test.md
$ find . -type f -iname *.tiff* | wc -l >> Test.md
and when I cat Test.md I get:
$ cat Test.md
13
10
8
and what I'm trying to do is:
JPG: 13
PNG: 10
TIFF: 8

So just add the string without a newline before the count.
: > Test.md # truncate the file
echo -n "JPG: " >> Test.md
find . -type f -iname '*.jpg*' | wc -l >> Test.md
echo -n "PNG: " >> Test.md
find . -type f -iname '*.png*' | wc -l >> Test.md
echo -n "TIFF: " >> Test.md
find . -type f -iname '*.tiff*' | wc -l >> Test.md
or like, grab the output of wc with command substitution, and pass to echo to do some formatting:
echo "JPG: $(find . -type f -iname '*.jpg*' | wc -l)" > Test.md
echo "PNG: $(find . -type f -iname '*.png*' | wc -l)" >> Test.md
echo "TIFF: $(find . -type f -iname '*.tiff*' | wc -l)" >> Test.md
Note: quote the *.jpg* argument for find inside single (or double) quotes to prevent filename expansion on the argument. find needs the argument with *, not literal filenames after the shell expansion.

What I would do using a here-doc :
cat<<EOF>Test.md
JPG: $(find . -type f -iname '*.jpg*' | wc -l)
PNG: $(find . -type f -iname '*.png*' | wc -l)
TIFF: $(find . -type f -iname '*.tiff*' | wc -l)
EOF

Related

How to count all the lines of files

I also need the directory name to be outputs as well. What I was able to do is to output the total number of lines in all directories with directory name.
find . -name '*.c' | xargs wc -l | xargs -I{} dirname {} | xargs -I{} dirname {}
I have jumbled up a mixture of bash commands mostly GNU-specific, make sure you have them, GNU grep and GNU Awk
find . -type f -print0 | xargs -0 grep -c ';$' | \
awk -F":" '$NF>0{cmd="dirname "$1; while ( ( cmd | getline result ) > 0 ) {printf "%s\t%s\n",result,$2} close(cmd) }'
The idea is grep -c returns the pattern count in format, file-name:count, which I am passing it to GNU Awk to filter those files whose count is greater than zero and print the directory of the file containing it and the count itself.
As a fancy one-liner as they call it these days,
find . -type f -print0 | xargs -0 grep -c ';$' | awk -F":" '$NF>0{cmd="dirname "$1; while ( ( cmd | getline result ) > 0 ) {printf "%s\t%s\n",result,$2} close(cmd) }'
Here is a script:
#!/usr/bin/env bash
for dir in */; do (
cd "$dir"
count=$(find . -name '*.c' -print0 | xargs -0 grep '[;]$' | wc -l)
echo -e "${count}\t${dir}"
) done
If you want numbers for each sub-directory:
#!/usr/bin/env bash
for dir in $(find . -type d); do (
cd "$dir"
count=$(find . -maxdepth 1 -name '*.c' -print0 | \
xargs -0 grep '[;]$' | wc -l)
echo -e "${count}\t${dir}"
) done
Using -maxdepth 1 makes sure the calculation is only done in the current directory, not its sub-directories. So each file is counted once.

Create tar gz on linux with specific list of files from sed output

Here is my command line:
find . -type f -exec file {} \; \
| sed 's/\(.*png\): .* \([0-9]* x [0-9]*\).*/\2 \1/' \
| sed 's/\(.*jpg\): .* \([0-9]*x[0-9]*\).*/\2 \1/' \
| awk 'int($1) < 1000' \
| sed 's/^.*[[:blank:]]//' \
| tar -czvf images.tar.gz --null -T -
And the error i got is:
tar: Unix\n./test.png\n./test2.jpg\n: Cannot stat: No such file or
directory
tar: Exiting with failure status due to previous errors
What i want is to find all images in current directory, who's width less than 1000 px and tar them into archive.
to use --null, you need to convert newlines to nulls first:
...
| tr '\n' '\0' \
| tar -czvf images.tar.gz --null -T -
(tested, working.)
also, here are a number of suggestions on speed and style in decreasing order of importance.
a. don't find and run file on more files than you need to:
find . -type f -iname "*.png" -or -iname "*.jpg"
b. for commands that can run on multiple files per command, such as file, use xargs to save a lot of time:
find . -type f -iname "*.png" -or -iname "*.jpg" -print0 | xargs -0 file
c. if you put | at the end of each line, you can continue on the next line without also using \.
find . -type f -iname "*.png" -or -iname "*.jpg" -print0 |
xargs -0 file
d. you can save yourself a lot of trouble since your max width is 999 by just greping for 1, 2, or 3 digit widths, though the awk '$1<1000' is ultimately better in case you ever want to use a different threshold:
find . -type f -iname "*.png" -or -iname "*.jpg" -print0 |
xargs -0 file |
grep ', [0-9][0-9]\?[0-9]\? x '
e. grep and awk are faster than sed, so use them where possible:
find . -type f -iname "*.png" -or -iname "*.jpg" -print0 |
xargs -0 file |
grep ', [0-9][0-9]\?[0-9]\? x ' |
grep -o -i '.*\.\(png\|jpg\)'
final command:
find . -type f -iname "*.png" -or -iname "*.jpg" -print0 |
xargs -0 file |
grep ', [0-9][0-9]\?[0-9]\? x ' |
grep -o -i '.*\.\(png\|jpg\)' |
tr '\n' '\0' |
tar -czvf images.tar.gz --null -T -
You can also use awk only with :
find . -type f \( -name "*.png" -or -name "*.jpg" \) -exec file {} \; | awk -v width_limit=1000 '
{
match($0, /,\s+([0-9]+)\s*x\s*([0-9]+)/, items)
if (items[1] < width_limit){
match($0, /(.*):/, filename)
print filename[1]
}
}' | tar -czvf allfiles.tar -T -
The width can be configured with width_limit variable
Quick way using perl:
find . -type f -exec file {} + |
perl -ne '
print $1."\0" if /^(.*):\s*(JPEG|PNG).*,\s*(\d+)\s+x\s*\d+\s*,/ &&
$3 < 1000;
' | tar -czvf images.tar.gz --null -T -
Using + operator to find as same effect than print0 | xargs -0.

Linux bash sum of two numbers from two commands

I have two commands which both return numbers.
For example:
cat `find -name \*.cd -print` | wc -l
cat `find -name \*.c -print` | wc -l
Let's say that the first one returns 10, the other 5.
What would the command which return the sum of them, without changing these commands, look like?
I need something like this:
cat `find -name \*.cd -print` | wc -l + cat `find -name \*.c -print` | wc -l
and it should return 15 in this case.
How I can do that?
That command will execute yours two commands and print the sum of the results.
echo $(($(cat `find -name \*.cd -print` | wc -l) + $(cat `find -name \*.c -print` | wc -l)))
EDIT:
As #Karoly Horvath commented it would be more readable if it's not a oneliner:
cd_count=$(cat `find -name \*.cd -print` | wc -l)
c_count=$(cat `find -name \*.c -print` | wc -l)
echo $(($cd_count + $c_count))
It's better to combine the two searches:
cat $(find -regex '.*\.cd?$') | wc -l
or
find -regex '.*\.cd?$' | xargs cat | wc -l
or, if you filenames can contain spaces:
find -regex '.*\.cd?$' -print0 | xargs -0 cat | wc -l
$ expr $(echo 5) + $(echo 10)
15
Just replace the echo statements with your commands.

issue Find command Linux

I have a folder and I want count all regular files in it, and for this I use this bash command:
find pathfolder -type f 2> err.txt | wc -l
In the folder there are 3 empty text files and a subfolder with inside it other text files.
For this reason I should get 3 as a result, but I get 6 and I don't understand why. Maybe there is some options that I did not set.
If I remove the subfolder I get 4 as result
To grab all the files and directories in current directory without dot files:
shopt -u dotglob
all=(*)
To grab only directories:
dirs=(*/)
To count only non-dot files in current directory:
echo $(( ${#all[#]} - ${#dirs[#]} ))
To do this with find use:
find . -type f -maxdepth 1 ! -name '.*' -exec printf '%.0s.\n' {} + | wc -l
Below solutions ignore the filenames starting with dot.
To count the files in pathfolder only:
find pathfolder -maxdepth 1 -type f -not -path '*/\.*' | wc -l
To count the files in ALL child directories of pathfolder:
find pathfolder -mindepth 2 -maxdepth 2 -type f -not -path '*/\.*' | wc -l
UPDATE: Converting comments into an answer
Based on the suggestions received from anubhava, by creating a dummy file using the command touch $'foo\nbar', the wc -l counts this filename twice, like in below example:
$> touch $'foo\nbar'
$> find . -type f
./foo?bar
$> find . -type f | wc -l
2
To avoid this, get rid of the newlines before calling wc (anubhava's solution):
$> find . -type f -exec printf '%.0sbla\n' {} +
bla
$> find . -type f -exec printf '%.0sbla\n' {} + | wc -l
1
or avoid calling wc at all:
$> find . -type f -exec sh -c 'i=0; for f; do ((i++)); done; echo $i' sh {} +
1

Bash find and expression

Is there some way to make this working?
pFile=find ${destpath} (( -iname "${mFile##*/}" )) -o (( -iname "${mFile##*/}" -a -name "*[],&<>*?|\":'()[]*" )) -exec printf '.' \;| wc -c
i need pFile return the number of file with the same filename, or if there aren't, return 0.
I have to do this, because if i only use:
pFile=find ${destpath} -iname "${mFile##*/}" -exec printf '.' \;| wc -c
It doesn't return if there are same filename with metacharacter.
Thanks
EDIT:
"${mFile##*/}" have as output file name in start folder without path.
echo "${mFile##*/}" -> goofy.mp3
Exmple
in start folder i have:
goofy.mp3 - mickey[1].avi - donald(2).mkv - scrooge.3gp
In destination folder i have:
goofy.mp3 - mickey[1].avi -donald(2).mkv -donald(1).mkv -donald(3).mkv -minnie.iso
i want this:
echo pFile -> 3
With:
pFile=find ${destpath} -iname "${mFile##*/}" -exec printf '.' \;| wc -c
echo pFile -> 2
With:
pFile=find ${destpath} -name "*[],&<>*?|\":'()[]*" -exec printf '.' \;| wc -c
echo pFile -> 4
With Same file name i mean:
/path1/mickey[1].avi = /path2/mickey[1].avi
I am not sure I understood your intended semantics of ${mFile##*/}, however looking at your start/destination folder example, I have created the following use case directory structure and the script below to solve your issue:
$ find root -type f | sort -t'/' -k3
root/dir2/donald(1).mkv
root/dir1/donald(2).mkv
root/dir2/donald(2).mkv
root/dir2/donald(3).mkv
root/dir1/goofy.mp3
root/dir2/goofy.mp3
root/dir1/mickey[1].avi
root/dir2/mickey[1].avi
root/dir2/minnie.iso
root/dir1/scrooge.3gp
Now, the following script (I've used gfind to indicated that you need GNU find for this to work, but if you're on Linux, just use find):
$ pFile=$(($(gfind root -type f -printf "%f\n" | wc -l) - $(gfind root -type f -printf "%f\n" | sort -u | wc -l)))
$ echo $pFile
3
I'm not sure this solves your issue, however it does print the number you expected in your provided example.

Resources