Mastering Linux: Essential Commands for File Navigation and Manipulation

Mastering Linux: Essential Commands for File Navigation and Manipulation

Photo by Lukas on Unsplash

In our previous posts, we delved into the structure of Linux, exploring its architecture and filesystem. In this entry, we'll shift our focus towards understanding Linux commands.

On launching a terminal session, we are initially located in the home directory. Let's explore some important commands to navigate and manipulate files and directories.

File and Directory Navigation Commands

  1. pwd: Print working directory

    • Syntax: pwd
  2. cd: Change directory is used to navigate between directories

    • Syntax: cd [directory]

    • ~ : Switches to the user's home directory

    • / : Changes to the root directory

    • - : Changes to the previous working directory

    • .. : Changes to the parent directory

    • dir1/dir2 : Changes to the directory within the directory

    • "dir 1" : Double quotes are used when special characters are present in the directory name

  3. ls: List contents of the directory

    • Syntax: ls [option] [directory]

    • -l : Provides long format listing

    • -t : Sorts files by modification time (newest first)

    • -r : Reverses the order of listing

    • -h : Displays file sizes in human-readable format (Kb, Mb, Gb)

    • -a : Includes hidden files (those beginning with .) in the listing

    • -s : Prints each file size in blocks

    • -S : Sorts files by size

    • -R : Lists subdirectories recursively

  4. find: To search for files in a directory hierarchy

    • Syntax: find [path/to/search] [expression]

    • -name : Searches by file name (e.g., find /path/to/search -name "filename")

    • -iname : Performs a case-insensitive search

    • -type : Searches by file type (e.g., find /path/to/search -type f for files and find /path/to/search -type d for directories)

    • -mtime : Finds files modified n days ago (e.g., find /path/to/search -mtime n)

    • -size : Searches for files of a specific size (e.g., find /path/to/search -size +50M for files larger than 50MB)

  5. locate: Find files by name. Unlike find, locate uses a database created by updatedb, resulting in faster search times. However, it may not always return the most recent results if the database has not been updated recently. Run updatedb to update the database.

    • Syntax: locate [option] pattern

    • -i : Ignores case sensitivity (locate -i "filename")

    • -l : Limits the number of matches returned (locate -l 10 "filename")

File and Directory Manipulation Commands

  1. mkdir: To create directories

    • Syntax: mkdir [option] directory

    • -p : Creates nested (parent) directories (mkdir -p parentdir/childdir{01..100})

    • -m : Sets file permissions for the new directory (mkdir -m 700 directoryname)

  2. touch: Create an empty file or update the timestamp of an exiting file

    • Syntax: touch [option] file

    • -a : Changes only the access time

    • -m : Changes only the modification time of the file.

    • -r : Takes the timestamp from an existing reference file.

    • -t : Set the timestamp manually. (touch -t [[CC]YY]MMDDhhmm[.ss] filename)

  3. rm: Remove files or directories

    • Syntax: rm [option] file

    • -r or -R : Remove directories and their contents recursively

    • -f (force) : Ignores nonexistent files and arguments, never prompts

    • -i (interactive) : Prompts for confirmation before removal

  4. cp: Copy files and directories

    • Syntax: cp [option] source destination

    • -r : Copies directories recursively

    • -i : Prompts before overwriting

    • -a : Preserves file attributes such as ownership, timestamps and file mode while copying

  5. mv: Move or rename files and directories

    • Syntax: mv [option] source destination

    • -i (Interactive) : Prompts before overwriting existing files

    • -u (Update) : Moves only when source file is newer than destination file or when file does not exist at destination

    • -n (no-clobber) : Prevents overwriting an existing file

  6. ln: Create links between files

    • Syntax: ln [option] source_file [link_name]

    • -s : Creates a symbolic link instead of hard links

    • -f : Forces the link creation by removing the existing link

  7. split: Split a file into pieces

    • Syntax: split [option] [input [prefix]]

    • -b : Specifies the size of each piece in bytes (e.g., split -b 100M file prefix)

    • -l : Specifies the number of lines per piece (e.g., split -l 1000 file prefix)

    • -d : Uses numeric suffixes instead of alphabetic

    • -a : Sets the length of the suffixes (e.g., split -a 4 file prefix)

File Viewing and Processing Commands

  1. cat: To view the file content, concatenate files and redirect output in terminal or files

    • Syntax: cat [option] [filename]

    • -n : Numbers all output lines

    • -b : Numbers all non-blank output lines

    • -s : Squeezes multiple adjacent blank lines

  2. head: Displays the beginning of a file

    • Syntax: head [option] [filename]

    • -n : Displays the first 'n' lines

    • -c : Displays the first 'n' bytes (e.g., head -c number filename)

  3. tail: To view the end of a file

    • Syntax: tail [option] [filename]

    • -n : Displays the last 'n' lines

    • -f : Follows the file as it is being updated

    • -c : Displays the last 'n' bytes (e.g., tail -c number filename)

  4. wc: To count the number of lines, words, characters and bytes in a file

    • Syntax: wc [option] [filename]

    • -l : Prints the line count

    • -w : Prints the word count

    • -c : Prints the byte count

  5. more: View file content one page at a time

    • Syntax: more [option] [filename]

    • -p (prompt): Does not scroll; instead, it clears the screen and then displays the text.

    • -c (clean): Does not scroll; instead, it paints each screen from the top, clearing the remainder of each line as it is displayed.

  6. less: Advanced file viewer. Allows backward navigation in the file as well as forward navigation.

    • Syntax: less [option] [filename]

      • -N : Displays line numbers at the start of each line

      • -E : Automatically exits upon reaching the end of the file

      • -S : Does not wrap long lines. To view the complete line, scroll horizontally

        While viewing files in less, you can use the following navigation keys:

        • f or Spacebar: Scroll forward one page

        • b: Scroll backward one page

        • d: Scroll forward half page

        • u: Scroll backward half page

        • Up arrow: Scroll up one line

        • Down arrow: Scroll down one line

        • g: Move to the beginning of the file

        • G: Move to the end of the file

        • /pattern: Search forward for the pattern

        • n: Search for next occurrence of the previous search

        • N: Search for the previous occurrence in reverse

        • ?: Search backward for the pattern

        • v: Edit the current file with the configured editor

        • q: Quit

Understanding fundamental linux commands can significantly enhance productivity when navigating and manipulating the filesystem. We will explore more linux commands in the next blog post.

Remember, the key to mastering Linux is practice and exploration. Don't be afraid to dive in and experiment. Happy learning!

Did you find this article valuable?

Support Pragna by becoming a sponsor. Any amount is appreciated!