🖥️ls

➡️This is a command-line reference manual for commands and command combinations that you don’t use often enough to remember it. This cheatsheet explains the ls command with important options and switches using examples.

▁ ▂ ▃ ▄ ꧁ 🔴☠ COMMANDLINE-KUNGFU WITH CHEATSHEETS ☠🔴꧂▅ ▃ ▂ ▁

#                ██╗     ███████╗
#                ██║     ██╔════╝
#                ██║     ███████╗
#                ██║     ╚════██║
#                ███████╗███████║
#                ╚══════╝╚══════╝
                                

# Displays everything in the target directory
ls path/to/the/target/directory

# Displays everything including hidden files
ls -a

# Displays all files, along with the size (with unit suffixes) and timestamp
ls -lh 

# Display files, sorted by size
ls -S

# Display directories only
ls -d */

# Display directories only, include hidden
ls -d .*/ */

#==============================#
# CMD LS 
#==============================##==============================#

\ls 
# This will run ls without using any alias called ls that might be in place. You can do this with any command.

ls -trl
# Dateien nach Zeit der Dateierzeugung anzeigen lassen

ls -turl
# Zeigt die Dateien an, auf die zuletzt zugegriffen wurde

ls -c
# Dateien nach Datum der letzten Änderung der Satusinformationen anzeigen lassen

ls -f
# Deaktiviert die Standardsortierung und Zeigt auch . und .. an

ls -S
# Sortiert nach Dateigröße

ls -t
# Sortiert nach dem Datum der letzten Änderung

ls -U
# Keine Sortierung

ls -u
# Sortiert nach Zugriffszeit

ls -X
# Sortiert nach Dateierweiterung

# How to Find Recent or Today’s Modified Files in Linux -> Using the ls command, you can only list today’s files in your home folder as follows, where:
ls  -al --time-style=+%D | grep 'date +%D'

ls -alX --time-style=+%D | grep 'date +%D'
# In addition, you can sort the resultant list alphabetically by including the -X flag:

ls -alS --time-style=+%D | grep 'date +%D'
# You can also list based on size (largest first) using the -S flag
    -a – list all files including hidden files
    -l – enables long listing format
    --time-style=FORMAT – shows time in the specified FORMAT
    +%D – show/use date in %m/%d/%y format
    
# List Files Based on Modification Time
# The below command lists files in long listing format, and sorts files based on modification time, newest first. To sort in reverse order, use '-r' switch with this command.
ls -lt

# List Files Based on Last Access Time
# Listing of files in directory based on last access time, i.e. based on time the file was last accessed, not modified.
ls -ltu

# List Files Based on Last Modification Time
# Listing of files in directory based on last modification time of file’s status information, or the 'ctime'. This command would list that file first whose any status information like: owner, group, permissions, size etc has been recently changed. If '-a' switch is used with above commands, they can list and sort even the hidden files in current directory, and '-r' switch lists the output in reverse order.

# For more in-depth sorting, like sorting on Output of find command, however ls can also be used, but there 'sort' proves more helpful as the output may not have only file name but any fields desired by user.

ls -ltc
#

ls -ld */*/
# Quickly list the directories that are two levels down without having to do something more complex with 'find'.

### Sorting Ouptut of ls -l based on Date
# This command sorts the output of 'ls -l' command based on 6th field month wise, then based on 7th field which is date, numerically.
ls -l | sort -k6M -k7n

ls | grep xargs | xargs grep ls
#

ls -lart | tail
#

ls -al|awk '{s+=$5}END{print s}'
#

ls -ldtr `find -type f`
#

ls -l /usr/local/ | grep '^d' |wc -l
#

ls -la --full-time |tr -s " " |cut -f6 -d " "|cut -c1-7 | sort | uniq -c
# Make month histogram of dates of files in current directory.

ls -lh | head -1
# Print human readable total size of just the files in the current directory. :)

ls -ltrah
# Print detailed list (-l) of all (-a) files reverse sorted (-r) by last modified time (-t) and with human readable size (-h)

ls -X 
# will group files by extension

ls -d .*/ */ 
# Show only the directories in the current directory. The / at the end of the wildcards make this work.

ls -lh | head -1 
# Print human readable total size of just the files in the current directory. :)

ls -lh | head -1 
# Print human readable total size of just the files in the current directory. :)

ls -X 
# will group files by extension.

ls -S
# List files in descending order of size: 

ls -la --full-time |tr -s " " |cut -f6 -d " "|cut -c1-7 | sort | uniq -c
#Make month histogram of dates of files in current directory.

ls -ld */*/
#Quickly list the directories that are two levels down without having to do something more complex with 'find'.

ls -Sr1 | while IFS=$'\n' read -r file; do xz "$file"; done
#Compress files with xz in PWD according to size, starting with smallest.

ls -ld ????
# Long list the files/directories with only 4 characters by using 4 match any single character patterns (?). 

ls -lh | head -1
# Print human readable total size of just the files in the current directory. :)

ls -ltrah
# Print detailed list (-l) of all (-a) files reverse sorted (-r) by last modified time (-t) and with human readable size (-h)

ls -Sr1 | while IFS=$'\n' read -r file; do xz "$file"; done
# Compress files with xz in PWD according to size, starting with smallest.

# Produce stats on how many photos you took on each day to help find the ones in large batches.
ls -1 20170*.JPG| cut -c1-8 | uniq -c 

\ls 
# This will run ls without using any alias called ls that might be in place. You can do this with any command.

ls -ld .*/ */ 
# Long list (-l) only the directories in the current directory. .*/ and */ are utilizing your shell's glob matching ability.

# How to Find Recent or Today’s Modified Files in Linux
###############################################################
### 1. Using the ls command, you can only list today’s files in your home folder as follows, where:
ls  -al --time-style=+%D | grep 'date +%D'

ls -alX --time-style=+%D | grep 'date +%D'
# In addition, you can sort the resultant list alphabetically by including the -X flag

ls -alS --time-style=+%D | grep 'date +%D'
# You can also list based on size (largest first) using the -S flag
#   -a – list all files including hidden files
#   -l – enables long listing format
#   --time-style=FORMAT – shows time in the specified FORMAT
#   +%D – show/use date in %m/%d/%y format

## 2. Again, it is possible to use the find command which is practically more flexible and offers plenty of options than ls, for the same purpose as below.
#   -maxdepth level is used to specify the level (in terms of sub-directories) below the starting point (current directory in this case) to 
# 			which the search operation will be carried out.
#   -newerXY, this works if timestamp X of the file in question is newer than timestamp Y of the file reference. X and Y represent any of the
# 			letters below:
#       a – access time of the file reference
#       B – birth time of the file reference
#       c – inode status change time of reference
#       m – modification time of the file reference
#       t – reference is interpreted directly as a time

ls -l | sort -k6M -k7n
## Sorting Ouptut of ls -l based on Date
# This command sorts the output of 'ls -l' command based on 6th field month wise, then based on 7th field which is date, numerically.

ls -lt
## List Files Based on Modification Time
# The below command lists files in long listing format, and sorts files based on modification time, newest first. To sort in reverse order, use
# '-r' switch with this command.

ls -ltu
## List Files Based on Last Access Time
# Listing of files in directory based on last access time, i.e. based on time the file was last accessed, not modified.

##List Files Based on Last Modification Time
# Listing of files in directory based on last modification time of file’s status information, or the 'ctime'. This command would list that file first whose any status information like: owner, group, permissions, size etc has been recently changed. If '-a' switch is used with above commands, they can list and sort even the hidden files in current directory, and '-r' switch lists the output in reverse order.

ls -ltc
# For more in-depth sorting, like sorting on Output of find command, however ls can also be used, but there 'sort' proves more helpful as the output may not have only file name but any fields desired by user.

# Here, gzip has below options:
# -> archive in verbose mode.
    -v 
# -> if there is already a file with the name filename.gz, that will be replaced.
    -f 

#						Path	         Criteria             Action
#
# Current directory        	(.)           	-name               -print
#
# Home directory     		(~)           	-type		     -exec
# Parent directory         	(..)			-links      
# Absolute Path            	/root/bin		-size
# Relative Path            	bin/tmp/      	-perm

ls *.jpg | grep -n ""  | sed 's,.*,0000&,' | sed 's,0*\(...\):\(.*\).jpg,mv "\2.jpg" "image-\1.jpg",' | sh
# rename all jpg files with a prefix and a counter 

ls 2017-0[5-9]-??.wav |cut -c 1-11 |date -f- +%A |sort|uniq -c|sort -nr 
# Stats for most common DOW for these wav files between May and Sept

ls -l 091?17*.jpg 
# The ? matches any one character in that place, * matches 0 or more of any character.

ls -Al Pictures/ | grep ^- | wc -l 
# Count the number of files in the Pictures directory, including hidden files; But skipping things like directories, symlinks and the total line. You can also use 'find Pictures/ -maxdepth 1 -type f | wc -l'
find t -maxdepth 1 -type f -printf '1\n' | wc -l 
	1 
or even 
find t -maxdepth 1 -type f -printf '1' | wc -c 
	1

	
ls -d .*/ */ 
# Show only the directories in the current directory. The / at the end of the glob wildcards make this trick work. Use LANG=C ls -d .*/ */ to put the hidden ones before the regular ones.

ls | md5sum 
# It's quick and dirty, but you can run this on two or more different directories across systems to *quickly* see if you have the same file names in each. The same hash value indicates you have the same file names. This doesn't compare contents of files of course.

# Determines latest pdf file downloaded by firefox in ~/Downloads directory
ls -tr ~/Downloads/*.pdf|tail -1

ls -ld */*/ 
# Quickly list the directories that are two levels down without having to do something more complex with 'find'.

ls -l . /lib/modules 
# Lists CWD and /lib/modules. Its not just there for decoration, you can use '.' (current directory) in your commands.

ls -ld */ 
# Using a / at the end of a glob pattern is a great trick for matching just directories. Long list non-hidden directories only.

ls -latrh
# Print detailed list (-l) of all (-a) files reverse sorted (-r) by last modified time (-t) and with human readable size (-h)

# Parsing and finding symbolic links in multiple paths
# This bash script will list simbolic links in multiple path (using wildchar * for nested directories), at level 1 from main directory, parsing the result as well. The script will use awk to parse returned data, getting the 11th data, space separated.    
ls -l `find ABC*/code/target-*/TARGET -maxdepth 1 -type l -name "*"` | awk '{print $11}'

ls -la --full-time | tr -s " " | cut -f6 -d " " | sort | uniq -c 
# Make histogram of dates of files in current directory. Thx @amenthes_de

ls -Sr1 | while IFS=$'\n' read -r file ; do gzip -v9 "$file" ; done 
# Compress files in CWD according to size, starting with smallest.

ls -ld *log* 
# Use the -d option when you want to see the directory itself instead of what is inside when you use wildcards like this.

ls -ltrah 
# Print detailed list (-l) of all (-a) files reverse sorted (-r) by last modified time (-t) and with human readable size (-h)

ls -d foo* 
# If your file globbing matches subdirectory entries, its helpful to use the -d option to list just the dir entries themselves.

# Get file exts of top 50 files by filesize in a folder (BSD)
# From http://superuser.com/questions/633752/how-to-extract-an-audio-track-from-an-mp4-video-file-on-windows
ls -s | sort | tail -r -n 50 | cut -d'.' -f 2 | sort | uniq

# Parsing and finding symbolic links in multiple paths - This bash script will list simbolic links in multiple path (using wildchar * for nested directories), at level 1 from main directory, parsing the result as well. The script will use awk to parse returned data, getting the 11th data, space separated.
ls -l `find ABC*/code/target-*/TARGET -maxdepth 1 -type l -name "*"` | awk '{print $11}'

          

# List files size sorted and print total size in a human readable format without sort, awk and other commands.
ls -sSh /path | head

        

# Tree-like output in ls
ls -R | grep ":$" | sed -e 's/:$//' -e 's/[^-][^\/]*\//--/g' -e 's/^/   /' -e 's/-/|/'
# Explanation: This one-liner initially does a recursive listing of the current directory: ls -R. Any output other that the directory names, identified by : at the very end of each line (hence :$), is filtered out: grep ":$". Finally there is a little of sed magic replacing any hierarchy level (/) with dashes (-).
# Limitations: Works for me with Bash under Linux, Mac OS X, Solaris.

#==============================##==============================#
# CMD LS						       #
#==============================##==============================#
░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░

  █║▌│║█║▌★ KALI ★ PARROT ★ DEBIAN 🔴 PENTESTING ★ HACKING ★ █║▌│║█║▌

              ██╗ ██╗ ██████╗  ██████╗ ██╗  ██╗███████╗██████╗
             ████████╗██╔══██╗██╔═══██╗╚██╗██╔╝██╔════╝██╔══██╗
             ╚██╔═██╔╝██║  ██║██║   ██║ ╚███╔╝ █████╗  ██║  ██║
             ████████╗██║  ██║██║   ██║ ██╔██╗ ██╔══╝  ██║  ██║
             ╚██╔═██╔╝██████╔╝╚██████╔╝██╔╝ ██╗███████╗██████╔╝
              ╚═╝ ╚═╝ ╚═════╝  ╚═════╝ ╚═╝  ╚═╝╚══════╝╚═════╝

               █║▌│║█║▌ WITH COMMANDLINE-KUNGFU POWER █║▌│║█║▌

░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░