docs

a slatepencil documentail site

View on GitHub

File management

List & sort

ls -l | head -6
ls -rlah  # r(reverse) a(all) h(human readable)
ls -d */ # Listing directories only
ls -l | sort -k3 | more # Listing files by owner
ls -l | sort -k4 # Listing files by group

find [starting location] [criteria] [options] [action to take]

# Finding files by name
find ~ -name "fix*" -print

# Finding files by type
find Documents -name junk* -type f -print
# To find a symbolic link, use "-type l"
find . -name "h*" -type l -ls

# Remove all files older than n days in current folder
find . -type f -mtime +n | xargs rm
# delete files (including subdirectories)
find ~ -name *.old -exec rm {} \;
find . -name *.old -ok rm {} \; # `-ok` ask for approval before removing the files. Answering y for “yes” would allow the find command to go ahead and remove the files one by one.
find . -name runme -okdir rm {} \; # use the `-okdir` option. Like `-ok`, this option will prompt for permission to run the command

# Finding files by owner and/or group
find . -user www-data -ls | head -4
find . -user 1000 -ls | head -4
find /tmp -user shs -ls 2> /dev/null
find /home -group tom -print
find /tmp -group 1000 -ls 2>/dev/null

# Finding files by file permissions
find /usr/bin -name "net*" -perm -g=w -ls
find /usr/bin -name "d*" -perm 777 -ls | head -3

# Finding files by age
# +100 (more than 100 days old) or -10 (modified within the last 10 days)
find Documents -mtime -1

# Finding files by size
find . -size 0 -ls | head -1 # this find empty files
find . -size 0 -ls | wc -l # this command would find a lot more empty files – representing the cache and such
find / -type f -size +1G -ls 2>/dev/null # finds files that are larger than 1 GB: Notice that the command sends all the “permission denied” messages to the /dev/null.

# `-execdir` runs the specified command from the directory in which the located file resides rather than from the directory in which the find command is run
find . -name runme -execdir pwd \;
/home/shs/bin
# If you run the command shown below and the directory contains a file named “ls”, it will run that file and it will run it even if the file does not have execute permissions set
find . -name runme -execdir ls \;
Running the /home/shs/bin/ls file

transfer files between hosts

# -P flag combines the flags --progress and --partial
# -z compression
rsync -azP source destination
rsync -a ~/dir1 username@remote_host:destination_directory
rsync -a username@remote_host:/home/username/dir1 place_to_sync_on_local_machine

# copy to a remote resource
scp code.c jolo@foo.edu:~/Project
# from a remote resource & rename
scp jolo@foo.edu:~/Project/output.txt ./Results/Run12_data.txt