Files, Directories, Text. CompTIA Linux+

Overview

Files and Directories

pwd - print name of current/working directory
touch - change file timestamps
mkdir - make directories
    -p, --parents - no error if existing, make parent directories as needed

ls - list directory contents
    -a, --all - do not ignore entries starting with
    -d, --directory - list directories themselves, not their contents
    -F, --classify - append indicator (one of * / = > @ |) to entries
    -i, --inode - print the index number of each file
    -l - use a long listing format
    -R, --recursive - list subdirectories recursively

cp - copy files and directories
    -a, --archive - same as -dR --preserve=all
    -f, --force [Overwrite any preexisting destination files with same name as DEST .] If an existing destination file cannot be opened, remove it and try again (this option is ignored when the -n option is also used)
    -i, --interactive - prompt before overwrite (overrides a previous -n option)
    -n, --no-clobber - do not overwrite an existing file (overrides a previous -i option)
    -R, -r, --recursive - copy directories recursively
    -u, --update - copy only when the SOURCE file is newer than the destination file or when the destination file is missing
    -v, --verbose - explain what is being done

mv - move (rename) files
    -f, --force - do not prompt before overwriting
    -i, --interactive - prompt before overwrite
    -n, --no-clobber - do not overwrite an existing file
    -u, --update - move only when the SOURCE file is newer than the destination file or when the destination file is missing
    -v, --verbose - explain what is being done

rsync - a fast, versatile, remote (and local) file-copying tool
    -a, --archive - archive mode; equals -rlptgoD (no -H,-A,-X)
    -D - same as --devices --specials
    -g, --group - preserve group
    -h, --human-readable - output numbers in a human-readable format
    -P --progress - show progress during transfer
    -l, --links - copy symlinks as symlinks
    -o, --owner - preserve owner (super-user only)
    -p, --perms - preserve permissions
    -r, --recursive - recurse into directories
    --stats - give some file-transfer stats [Display detailed file transfer statistics]
    -t, --times - preserve modification times
    -v, --verbose - increase verbosity

rm - remove files or directories
    -d, --dir - remove empty directories
    -f, --force - ignore nonexistent files and arguments, never prompt
    -i - prompt before every removal
    -I - prompt once before removing more than three files, or when removing recursively; less intrusive than -i, while still giving protection against most mistakes
    -r, -R, --recursive - remove directories and their contents recursively
    -v, --verbose - explain what is being done [Provide detailed command action information as command executes.]

rmdir - remove empty directories


Linking Files and Directories

Hard Link A hard link is a file or directory that has one index (inode) number but at least two different file names. A hard link allows you to have a pseudo-copy of a file without truly copying its data. This is often used in file backups where not enough filesystem space exists to back up the file’s data.
ln OriginalFile.txt HardLinkFile.txt

When creating and using hard links, there are a few important items to remember:

  • The original file must exist before you issue the ln command.
  • The second file name listed in the ln command must not exist prior to issuing the command.
  • An original file and its hard links share the same inode number.
  • An original file and its hard links share the same data.
  • An original file and any of its hard links can exist in different directories.
  • An original file and its hard links must exist on the same filesystem.

Soft Link Typically, a soft link file provides a pointer to a file that may reside on another filesystem.
ln -s OriginalSFile.txt SoftLinkFile.txt

When creating and using soft links, there are a few important items to remember:

  • The original file must exist before you issue the ln -s command.
  • The second file name listed in the ln -s command must not exist prior to issuing the command.
  • An original file and its soft links do not share the same inode number.
  • An original file and its soft links do not share the same data.
  • An original file and any of its soft links can exist in different directories.
  • An original file and its softs links can exist in different filesystems.

readlink - print resolved symbolic links or canonical file names
readlink -f /bin/bash


Reading Files

cat - concatenate files and print on the standard output
    -n, --number - number all output lines
    
-v, --show-nonprinting
- use ^ and M- notation, except for LFD and TAB

pr - convert text files for printing
    -COLUMN, --columns=COLUMN - output COLUMN columns and print columns down, unless -a is used. Balance number of lines in the columns on each page
    -n[SEP[DIGITS]], --number-lines[=SEP[DIGITS]] - number lines, use DIGITS (5) digits, then SEP (TAB), default counting starts with 1st line of input file
    -l, --length=PAGE_LENGTH - [Change the default 66-line page length to n lines long.] set the page length to PAGE_LENGTH (66) lines (default number of lines of text 56, and with -F 63). implies -t if PAGE_LENGTH <= 10
    -m, --merge - print all files in parallel, one in each column, truncate lines, but join lines of full length with -J
    -s[CHAR], --separator[=CHAR] - separate columns by a single character, default for CHAR is the character without -w and 'no char' with -w. -s[CHAR] turns off line truncation of all 3     -t, --omit-header - omit page headers and trailers; implied if PAGE_LENGTH <= 10 [Do not display any file header or trailers.]     -w, --width=PAGE_WIDTH - set page width to PAGE_WIDTH (72) characters for multiple text-column output only, -s[char] turns off (72) https://www.geeksforgeeks.org/pr-command-in-linux/

grep, egrep, fgrep, rgrep - print lines that match patterns [-i, --ignore-case]
head - output the first part of files (head -n 2 /etc/passwd)
tail - output the last part of files (tail -f /var/log/auth.log)

more - is a filter for paging through text one screenful at a time.
less - opposite of more. Less does not have to read the entire input file before starting, so with large input files it starts up faster than text editors like vi (1).


Finding Information

diff - compare files line by line
    -e, --ed - [Create an ed script, which can be used to make the first file compared the same as the second file compared.]
    -q, --brief - report only when files differ
    -r, --recursive - recursively compare any subdirectories found
    -s, --report-identical-files - report when two files are the same
    -W, --width=NUM - output at most NUM (default 130) print columns
    -y, --side-by-side - output in two columns

which - locate a command
whereis - locate the binary, source, and manual page files for a command

locate - find files by name, utility searches a database, mlocate.db, which is located in the /var/lib/mlocate/
    -A, --all - Print only entries that match all PATTERNs instead of requiring only one of them to match.
    -b, --basename - Match only the base name against the specified patterns. This is the opposite of --wholename.
    -c, --count - Instead of writing file names on standard output, write the number of matching entries only.
    -i, --ignore-case - Ignore case distinctions when matching patterns.
    -q, --quiet - Write no messages about errors encountered while reading and processing databases.
    -r, --regexp REGEXP - Search for a basic regexp REGEXP. No PATTERNs are allowed if this option is used, but this option can be specified multiple times.
    -w, --wholename - Match only the whole path name against the specified patterns. [Display file names that match the pattern and include any directory names that match the pattern. This is default behavior.]

Using the locate command PATTERN can be a little tricky, due to default pattern file globbing . File globbing occurs when you use wildcards, such as an asterisk (*) or a question mark (?) added to a file name argument in a command, and the file name is expanded into multiple names. For example, passw*d could be expanded into the file name password or passwrd.
If you don’t enter any wildcards into your pattern, the locate command, by default, adds wildcards to the pattern. So if you enter the pattern, passwd, it is automatically turned into passwd. Thus, if you just want to search for the base name passwd, with no file globbing, you must add quotation marks (single or double) around the pattern and precede the pattern with the \ character.
locate -b '\passwd' '\group'
updatedb - update a database for mlocate

find - search for files in a directory hierarchy
    -cmin n : Display names of files whose status changed n minutes ago.
    -empty : Display names of files that are empty and are a regular text file or a directory.
    -gid n : Display names of files whose group id is equal to n.
    -group name : Display names of files whose group is name.
    -inum n : Display names of files whose inode number is equal to n.
    -maxdepth n : When searching for files, traverse down into the starting point directory’s tree only n levels.
    -mmin n : Display names of files whose data changed n minutes ago.
    -name pattern : Display names of files whose name matches pattern. Many regular expression arguments may be used in the pattern and need to be enclosed in quotation marks to avoid unpredictable results. Replace -name with -iname to ignore case.
    -nogroup : Display names of files where no group name exists for the file’s group ID.
    -nouser : Display names of files where no username exists for the file’s user ID.
    -perm mode : Display names of files whose permissions matches mode. Either octal or symbolic modes may be used.
    -size n : Display names of files whose size matches n. Suffixes can be used to make the size more human readable, such as G for gigabytes.
    -user name : Display names of files whose owner is name.
find /usr/bin -perm /4000