Find and order all files by size, save the result to a text file on a Linux system
du -k | sort -nr | awk '
BEGIN {
split("KB,MB,GB,TB", Units, ",");
}
{
u = 1;
while ($1 >= 1024) {
$1 = $1 / 1024;
u += 1
}
$1 = sprintf("%.1f %s", $1, Units[u]);
print $0;
}
' > result_list_ordered.txt
My personal Linux notes, a work in progress, shared online with the world. From intro to pro Linux Administration.
Showing posts with label Find. Show all posts
Showing posts with label Find. Show all posts
Wednesday, May 1, 2013
Find files by modification date, within a modified by date range on Linux
Find files modified within a date range
Use these commands
touch --date "2013-04-29" /tmp/start
touch --date "2013-05-1" /tmp/end
find -type f -newer /tmp/start -not -newer /tmp/end
Use these commands
touch --date "2013-04-29" /tmp/start
touch --date "2013-05-1" /tmp/end
find -type f -newer /tmp/start -not -newer /tmp/end
Labels:
Date,
Date Range,
Find,
Modified Date,
Search,
touch
Search or find all users on a Linux system
List all users on a Linux OS
Try one of these:
# cut -d: -f1 /etc/passwd
# cat /etc/passwd | cut -d ":" -f1
Got other ideas on how to do this? please leave a comment
Thanks!
Try one of these:
# cut -d: -f1 /etc/passwd
# cat /etc/passwd | cut -d ":" -f1
Got other ideas on how to do this? please leave a comment
Thanks!
Search files for a string in linux
Use grep
example, look for the text hsbc in the /srv/www/ location
# grep -r -l "hsbc" /srv/www/
example, look for the text hsbc in the /srv/www/ location
# grep -r -l "hsbc" /srv/www/
Tuesday, February 19, 2013
Find the Size of Directory in Suse Linux
Try these
du -h ./
For the most part this works. If you have spaces in file/directory
names than you may get some errors, but I'm sure you can work around
those if needed:
ls -1drt *| xargs du -h --summarize
You could also try
du -hc --max-depth=1
This will give you all the directories in the current directory, and a total at the bottom. Play with the --max-depth to dig down further in the structure and get more fine-grained results.
You can run this with a pipe to grep "G" to do a quick search of folders that are large enough to be measured in gigabytes. It will pull any directories with a capital G in them, but there aren't many of those so it works for me.
du -hc --max-depth=1 | grep "G"
du -h ./
For the most part this works. If you have spaces in file/directory
names than you may get some errors, but I'm sure you can work around
those if needed:
ls -1drt *| xargs du -h --summarize
You could also try
du -hc --max-depth=1
This will give you all the directories in the current directory, and a total at the bottom. Play with the --max-depth to dig down further in the structure and get more fine-grained results.
You can run this with a pipe to grep "G" to do a quick search of folders that are large enough to be measured in gigabytes. It will pull any directories with a capital G in them, but there aren't many of those so it works for me.
du -hc --max-depth=1 | grep "G"
Friday, February 15, 2013
Where's the php.ini file
/etc/php.ini
Or here:
/etc/php/php.ini
/etc/php5/php.ini
Or here:
/usr/bin/php5/bin/php.ini
Anyway, you can always find any file named php.ini in this manner
find / -name php.ini
The simplest yet most powerful usage of the renowned find command. By its help I was able to locate the php.ini on my Ubuntu 9.04 Apache2 and PHP5:
/etc/php5/apache2/php.iniFriday, December 28, 2012
man find
Man page for the find command
find [pathnames] [conditions]
An extremely useful command for finding particular groups of files (numerous examples follow this description). find descends the directory tree beginning at each pathname and locates files that meet the specified conditions. The default pathname is the current directory. The most useful conditions include -name and -type (for general use), -exec and -size (for advanced use), and -mtime and -user(for administrators).
Conditions may be grouped by enclosing them in \( \) (escaped parentheses), negated with !, given as alternatives by separating them with -o, or repeated (adding restrictions to the match; usually only for -name, -type, or -perm). Note that "modification" refers to editing of a file's contents, whereas "change" means a modification, or permission or ownership changes. In other words, -ctime is more inclusive than -atime or -mtime. -amin +n| -n| n -anewer file -atime +n| -n| n -cmin +n| -n| n -cnewer file -ctime +n| -n| n -daystart -depth -empty -exec command{ } \ ; -false -follow -fstype type -gid num -group gname -ilname pattern -iname pattern -inum n -ipath pattern -iregex pattern -links n -lname pattern -maxdepth num -mindepth num -mmin +n| -n| n -mount, -xdev -mtime +n| -n| n -name pattern -newer file -nogroup -noleaf -nouser -ok command { }\; -path pattern -perm nnn -print -regex pattern -size n[c] -type c -user user
find $HOME -print
List all files named chapter1 in the /work directory:
find /work -name chapter1
List all files beginning with memo owned by ann:
find /work -name 'memo*' -user ann -print
Search the filesystem (begin at root) for manpage directories:
find / -type d -name 'man*' -print
Search the current directory, look for filenames that don't begin with a capital letter, and send them to the printer:
find . \! -name '[A-Z] *' -exec lpr { }\;
Find and compress files whose names don't end with .gz:
gzip `find . \! -name '*.gz' -print`
Remove all empty files on the system (prompting first):
find / -size 0 -ok rm { } \;
Search the system for files that were modified within the last two days (good candidates for backing up):
find / -mtime -2 -print
Recursively grep for a pattern down a directory tree:
find /book -print | xargs grep '[Nn] utshell'
If the files kt1 and kt2 exist in the current directory, their names can be printed with the command:
$ find . -name 'kt[0-9] ' ./kt1 ./kt2
Since the command prints these names with an initial ./ path, you need to specify the ./ when using the-path option:
$ find . -path './kt[0-9] ' ./kt1 ./kt2
The -regex option uses a complete pathname, like -path, but treats the following argument as a regular expression rather than a glob pattern (although in this case the result is the same):
$ find . -regex './kt[0-9] ' ./kt1 ./kt2
Source: http://www.oreillynet.com/linux/cmd/cmd.csp?path=f/find
find
An extremely useful command for finding particular groups of files (numerous examples follow this description). find descends the directory tree beginning at each pathname and locates files that meet the specified conditions. The default pathname is the current directory. The most useful conditions include -name and -type (for general use), -exec and -size (for advanced use), and -mtime and -user(for administrators).
Conditions may be grouped by enclosing them in \( \) (escaped
Conditions and actions
Find files last accessed more than n (+n), less than n (-n), or exactly n minutes ago.
Find files that were accessed after file was last modified. Affected by -follow when after -follow on the command line.
Find files that were last accessed more than n (+n), less than n (-n), or exactly n days ago. Note that find changes the access time of directories supplied as pathnames.
Find files last changed more than n (+n), less than n (-n), or exactly n minutes ago.
Find files that were changed after they were last modified. Affected by -follow when after -follow on the command line.
Find files that were changed more than n (+n), less than n (-n), or exactly n days ago. A change is anything that changes the directory entry for the file, such as a chmod.
Calculate times from the start of the day today, not 24 hours ago.
Descend the directory tree, skipping directories and working on actual files first, and then the parent directories. Useful when files reside in unwritable directories (e.g., when usingfind with cpio).
Continue if file is empty. Applies to regular files and directories.
Run the Linux command, from the starting directory on each file matched by find (providedcommand executes successfully on that file—i.e., returns a 0 exit status). When commandruns, the argument { } substitutes the current file. Follow the entire sequence with an escaped semicolon (\;). In some shells, the braces may need to be escaped as well.
Return false value for each file encountered.
Follow symbolic links and track the directories visited (don't use with -type l).
Match files only on type filesystems. Acceptable types include minix, ext, ext2, xia, msdos,umsdos, vfat, proc, nfs, iso9660, hpfs, sysv, smb, and ncpfs.
Find files with numeric group ID of num.
Find files belonging to group gname. gname can be a group name or a group ID number.
A case-insensitive version of -lname.
A case-insensitive version of -name.
Find files whose inode number is n.
A case-insensitive version of -path.
A case-insensitive version of -regex.
Find files having n links.
Search for files that are symbolic links, pointing to files named pattern. pattern can include shell metacharacters and does not treat / or. specially. The match is case-insensitive.
Do not descend more than num levels of directories.
Begin applying tests and actions only at levels deeper than num levels.
Find files last modified more than n (+n), less than n (-n), or exactly n minutes ago.
Search only for files that reside on the same filesystem as pathname.
Find files that were last modified more than n (+n), less than n (-n), or exactly n days ago. A modification is a change to a file's data.
Find files whose names match pattern. Filename metacharacters may be used but should be escaped or quoted.
Find files that were modified more recently than file; similar to -mtime. Affected by -followonly if it occurs after -follow on the command line.
The file's group ID does not correspond to any group.
Normally, find assumes that each directory has at least two hard links that should be ignored (a hard link for its name and one for "."--i.e., two fewer "real" directories than its hard link count indicates). -noleaf turns off this assumption, a useful practice when findruns on non-Unix-style filesystems. This forces find to examine all entries, assuming that some might prove to be directories into which it must descend (a time-waster on Unix).
The file's user ID does not correspond to any user.
Same as -exec, but prompts user to respond with y before command is executed.
Find files whose names match pattern. Expect full pathnames relative to the starting pathname (i.e., do not treat / or . specially).
Find files whose permission flags (e.g., rwx) match octal number nnn exactly (e.g., 664 matches -rw-rw-r--). Use a minus sign before nnn to make a "wildcard" match of any unspecified octal digit (e.g., -perm -600 matches -rw-******, where * can be any mode).
Print the matching files and directories, using their full pathnames. Return true. This is the default behavior.
Like -path, but uses grep-style regular expressions instead of the shell-like globbing used in -name and -path.
Find files containing n blocks, or if c is specified, n characters long.
Find files whose type is c. c can be b (block special file), c (character special file), d(directory), p (fifo or named pipe), l (symbolic link), s (socket), or f (plain file).
Find files belonging to user (name or ID).
Examples
List all files (and subdirectories) in your home directory:find $HOME -print
List all files named chapter1 in the /work directory:
find /work -name chapter1
List all files beginning with memo owned by ann:
find /work -name 'memo*' -user ann -print
Search the filesystem (begin at root) for manpage directories:
find / -type d -name 'man*' -print
Search the current directory, look for filenames that don't begin with a capital letter, and send them to the printer:
find . \! -name '[A-Z] *' -exec lpr { }\;
Find and compress files whose names don't end with .gz:
gzip `find . \! -name '*.gz' -print`
Remove all empty files on the system (prompting first):
find / -size 0 -ok rm { } \;
Search the system for files that were modified within the last two days (good candidates for backing up):
find / -mtime -2 -print
Recursively grep for a pattern down a directory tree:
find /book -print | xargs grep '[Nn] utshell'
If the files kt1 and kt2 exist in the current directory, their names can be printed with the command:
$ find . -name 'kt[0-9] ' ./kt1 ./kt2
Since the command prints these names with an initial ./ path, you need to specify the ./ when using the-path option:
$ find . -path './kt[0-9] ' ./kt1 ./kt2
The -regex option uses a complete pathname, like -path, but treats the following argument as a regular expression rather than a glob pattern (although in this case the result is the same):
$ find . -regex './kt[0-9] ' ./kt1 ./kt2
Source: http://www.oreillynet.com/linux/cmd/cmd.csp?path=f/find
Search & Find files on SuSe
If you installed the package "locate", you'll be able to search an
indexed list of all files on your computer to find where they are.
locate [filename]
The slow method is "find"...as in:
find / -type f -name [filename]
find / | grep [filename]
Note: these commands are executed in a Terminal.
locate [filename]
The slow method is "find"...as in:
find / -type f -name [filename]
find / | grep [filename]
Note: these commands are executed in a Terminal.
Subscribe to:
Posts (Atom)