Find file in Linux then report the size of file searched [duplicate] - linux

This question already has answers here:
Find files of specified size using bash in Unix [closed]
(3 answers)
Closed 9 years ago.
How can i search for file in Linux then report the size of those file i have found ?
For example, i want to search file named core.txt in my home directory in Linux, core.txt also appear on some sub-directory under my home dir. Then after found core.txt, the command should also show me file size of those files have founded.
Cheers

We can use find command to find the file and du -sh to find out its size.
We will execute du -sh on found files. So final command would be
find ~ -name "core.txt" -exec du -sh {} \;
or
find ~ -name "core.txt" | xargs du -sh
In 2nd command xargs will not handle spaces in file name. So We can tell exact delimiter to xargs to handle spaces in file name.
find ~ -name "core.txt" | xargs -d '\n' du -sh

Related

Listing only name and size of the file with largest size (UNIX) [duplicate]

This question already has answers here:
How to find the largest file in a directory and its subdirectories?
(17 answers)
Closed last year.
How can I only list the name and the size of the largest file size in a directory I used this command but It didn't work when I tried on a different directories.
find . -type f -exec ls -lS {} \; | sort -t" " -n -k5 | cut -d" " -f5,10 | tail -n1
This should work
find . -maxdepth 1 -printf '%s %p\n'|sort -nr|head -n 1
the number 1 after head -n means how many of the largest files it'll output and if you want to find files in sub-directories as well you can do that by removing the -maxdepth 1 or changing the number to a larger one.
for more info see the replies in this earlier post: How to find the largest file in a directory and its subdirectories?
For posts about this kind of stuff i suggest tagging them as bash, sh or shell.

Search entire server for a certain file type larger than 1GB in size? [duplicate]

This question already has answers here:
Find files with a certain extension that exceeds a certain file size
(3 answers)
Closed 3 years ago.
I have the following linux command I use to determine if a directory is larger than 1GB in size:
du -sh * | sort -hr | awk '$1 ~ /[GT]/
How would I modify this to instead search for any file that has a certain file type, such as .log filetype?
Better use find :
find . -type f -name '*.log' -size +1G
sudo find /www-data -name "*.log" -type f -size +1000000k -exec ls -lh {} \; | awk '{ print $9 ": " $5 }' | sort

How can I search for specific file contents in all files in both current folder and all subfolders [duplicate]

This question already has answers here:
How do I recursively grep all directories and subdirectories?
(26 answers)
Closed 4 years ago.
Using Mac terminal server linux/bash commands, how can I search for a particular text string in all *.txt files in the current folder plus all files in the subfolders inside the current folder?
grep -i "xxx" */*
xxx is the target text I am trying to find.
find . -type f -print | egrep ".txt$" | xargs grep "SearchPatern"
Explained as
Find all the file names in the current directory and below send to ....
grep which picks out the file name that end in .txt and send names to ....
xargs which will execute a grep command on each file to look for SearchPatern.

Removing files in a sub directory based on modification date [duplicate]

This question already has answers here:
bash script to remove directories based on modified file date
(3 answers)
Closed 8 years ago.
Hi so I'm trying to remove old backup files from a sub directory if the number of files exceeds the maximum and I found this command to do that
ls -t | sed -e '1,10d' | xargs -d '\n' rm
And my changes are as follows
ls -t subdirectory | sed -e '1,$f' | xargs -d '\n' rm
Obviously when I try running the script it gives me an error saying unknown commands: f
My only concern right now is that I'm passing in the max number of files allowed as an argument so I'm storing that in f but now I'm not too sure how to use that variable in the command above instead of having to set condition to a specific number.
Can anyone give me any pointers? And is there anything else I'm doing wrong?
Thanks!
The title of your question says "based on modification date". So why not simply using find with mtime option?
find subdirectory -mtime +5d -exec rm -v {} \;
Will delete all files older than 5 days.
The problem is that the file list you are passing to xargs does not contain the needed path information to delete the files. When called from the current directory, no path is needed, but if you call it with subdirectory, you need to then rm subdirectory/file from the current directory. Try it:
ls -t subdirectory # returns files with no path info
What you need to do is change to the subdirectory, call the removal script, then change back. In one line it could be done with:
pushd subdirectory &>/dev/null; ls -t | sed -e '1,$f' | xargs -d '\n' rm; popd
Other than doing it in a similar manner, you are probably better writing a slightly longer and more flexible script forming the list of files with the find command to insure the path information is retained.

How to use du to take the paths from stdin and calculate the total size? [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Get total size of folders with find & du
I can use du like this to get the file size of each file from stdin
find . -name "*.java" -exec du -h {} \;
But I can't get the total size.. Does anyone have ideas about that?
Thanks!
Answered here:
How to get total size of folders with find and du?
Use xargs(1) instead of -exec:
find . -name bak -type d | xargs du -ch
executes the command for each file found (check the find(1) documentation). Piping to xargs lets you aggregate those filenames and
only run du once.
In your case it would be:
find . -name "*.java" | xargs du -ch
Options:
-c, --total
produce a grand total

Resources