Mastering Linux: Essential Commands for File Navigation and Manipulation
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
pwd
: Print working directory- Syntax:
pwd
- Syntax:
cd
: Change directory is used to navigate between directoriesSyntax:
cd [directory]
~
: Switches to the user's home directory/
: Changes to the root directory-
: Changes to the previous working directory..
: Changes to the parent directorydir1/dir2
: Changes to the directory within the directory"dir 1"
: Double quotes are used when special characters are present in the directory name
ls
: List contents of the directorySyntax:
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
find
: To search for files in a directory hierarchySyntax:
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 andfind /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)
locate
: Find files by name. Unlikefind
,locate
uses a database created byupdatedb
, resulting in faster search times. However, it may not always return the most recent results if the database has not been updated recently. Runupdatedb
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
mkdir
: To create directoriesSyntax:
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
)
touch
: Create an empty file or update the timestamp of an exiting fileSyntax:
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
)
rm
: Remove files or directoriesSyntax:
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
cp
: Copy files and directoriesSyntax:
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
mv
: Move or rename files and directoriesSyntax:
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
ln
: Create links between filesSyntax:
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
split
: Split a file into piecesSyntax:
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
cat
: To view the file content, concatenate files and redirect output in terminal or filesSyntax:
cat [option] [filename]
-n
: Numbers all output lines-b
: Numbers all non-blank output lines-s
: Squeezes multiple adjacent blank lines
head
: Displays the beginning of a fileSyntax:
head [option] [filename]
-n
: Displays the first 'n' lines-c
: Displays the first 'n' bytes (e.g.,head -c number filename
)
tail
: To view the end of a fileSyntax:
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
)
wc
: To count the number of lines, words, characters and bytes in a fileSyntax:
wc [option] [filename]
-l
: Prints the line count-w
: Prints the word count-c
: Prints the byte count
more
: View file content one page at a timeSyntax:
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.
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 horizontallyWhile viewing files in
less
, you can use the following navigation keys:f
orSpacebar
: Scroll forward one pageb
: Scroll backward one paged
: Scroll forward half pageu
: Scroll backward half pageUp arrow: Scroll up one line
Down arrow: Scroll down one line
g
: Move to the beginning of the fileG
: Move to the end of the file/pattern
: Search forward for the patternn
: Search for next occurrence of the previous searchN
: Search for the previous occurrence in reverse?
: Search backward for the patternv
: Edit the current file with the configured editorq
: 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!