Linux navigation
What is the Shell?
Simply put, the shell is a program that takes commands from the keyboard and gives them to the operating system to perform. It allows users to interact with the operating system by typing commands, which the shell interprets and executes.
The shell serves as both a command interpreter and a scripting language. It provides a way to start programs, manage files and directories, and automate tasks through scripts. Shell scripts can include various programming constructs, like loops, conditionals, and functions, making them powerful tools for automating repetitive tasks or managing system configurations.
On most Linux systems a program called bash (which stands for Bourne Again Shell, an enhanced version of the original Unix shell program, sh, written by Steve Bourne) Besides bash, there are other shell programs available for Linux systems. These include: ksh, tcsh, and zsh.
What is a Terminal?
A terminal, also known as a terminal emulator, is a software application that provides a text-based interface to interact with the operating system. It allows users to access the shell and enter commands to perform various tasks.
Shell Navigation
The files on a Linux system are arranged in what is called a hierarchical directory structure. This means that they are organized in a tree-like pattern of directories which may contain files and subdirectories. The first directory in the file system is called the root directory. The root directory contains files and subdirectories, which contain more files and subdirectories, and so on.
The command line interface cannot provide graphic pictures of the file system structure. The directory we are standing in is called the working directory.
To see the name of the working directory, we use the pwd command.
pwd command:
pwd - Print the name of the current working directory.
To list the files in the working directory, we use the ls command.
ls command:
List information about the FILEs (the current directory by default).? Sort entries alphabetically if no option or --sort is specified.
syntax: ls [OPTION]... [FILE]...
Options:
ls -l: use a long listing format.
ls -a: do not ignore entries starting with. (Includes hidden files).
ls -R: list subdirectories recursively.
ls -r: reverse order while sorting.
ls -s: print the allocated size of each file, in blocks.
ls -S: sort by file size, largest first.
If we use the -l option with ls, you will get a file listing that contains a wealth of information about the files being listed. For example:
cd command:
To change the working directory we use the cd command. To do this, we type cd followed by the pathname of the desired working directory. A pathname is the route we take along the branches of the tree to get to the directory we want. Pathnames can be specified in two different ways; absolute pathnames or relative pathnames.
An absolute pathname: begins with the root directory and follows the tree branch by branch until the path to the desired directory or file is completed. A relative pathname: starts from the working directory
The "." notation refers to the working directory itself and the ".." notation refers to the working directory's parent directory.
If we type cd followed by nothing, cd will change the working directory to our home directory. Typing cd - changes the working directory to the previous one.
Typing cd ~username: will change the working directory to the home directory of the specified user.
less command
less is a program that lets us view text files. This is very handy since many of the files used to control and configure Linux are human-readable.
The less program is invoked by simply typing: less text_file
Scrolling:
j or ↓ (Down Arrow): Scroll down one line.
k or ↑ (Up Arrow): Scroll up one line.
Space: Scroll down one screen.
b: Scroll up one screen.
G: Go to the end of the file.
g: Go to the beginning of the file.
Searching:
/pattern: Search forward for pattern.
?pattern: Search backward for pattern.
n: Repeat the last search in the same direction.
N: Repeat the last search in the opposite direction.
Other Useful Commands:
q: Quit less.
h: Display help and a summary of commands.
&pattern: Display only lines that match the pattern.
file command
The file command in Unix and Unix-like systems is used to determine the type of a file. It examines the file's contents and provides information about its format, such as whether it is a text file, binary file, executable, script, image, archive, etc.
The file command works by checking the file's "magic numbers," which are specific byte sequences at the beginning of a file that indicates its type. It uses a database of these magic numbers to make its determination. If no magic number is found, the file may look at the file's extension and contents to make an educated guess.
Option:
file -b (brief): Omits the filename in the output, showing only the file type.
file -i (mime type): Displays the file type as a MIME type.
file -s (special files): Reads block or character special files.
file -L (dereference): Follows symbolic links to determine the type of the target file.
file -h: Reports on the link itself rather than the file it points to (if the file is a symbolic link).
Manipulating Files
Manipulating files in Linux involves creating, copying, moving, renaming, deleting, and editing files. Here are some essential commands and tools for file manipulation in a Linux environment:
领英推荐
Creating files:
touch command: Creates an empty file or updates the timestamp of an existing file.
echo or printf: Creates a file with some initial content.
with echo command: The command echo > is used to overwrite a file, meaning that it will replace any existing content in that file with the new content you provide. On the other hand, echo >> is used to append content to a file, meaning that it adds new content to the end of the file without removing the existing content.
Creating directories:
The mkdir command is used to create directories.
Options:
mkdir -m, --mode=MODE: set file mode (as in chmod), not a=rwx – umask.
mkdir-p, --parents: no error if existing, make parent directories as needed
Viewing File Contents:
cat command: Displays the entire content of a file.
less or more command: Allows scrolling through the file content.
head command: Displays the first few lines of a file.
tail command: Displays the last few lines of a file.
Copying Files:
cp command: copy files and directories.
Options:
cp --attributes-only:? don't copy the file data, just the attributes.
cp --backup[=CONTROL]: backup each existing destination file.
cp --copy-contents: copy contents of special files when recursive.
cp -i, --interactive: prompt before overwriting.
cp -R, -r, --recursive: copy directories recursively.
cp -n, --no-clobber: do not overwrite an existing file.
Moving and Renaming Files:
The mv command moves or renames files and directories depending on how it is used. It will either move one or more files to a different directory, or it will rename a file or directory.
To rename a file use: mv filename1 filename2 If file2 does not exist, then file1 is renamed file2. If file2 exists, its contents are silently replaced with the contents of file1.
To move files (and/or directories) to a different directory: mv file1 file2 dir1The files file1 and file2 are moved to directory dir1. If dir1 does not exist, mv will exit with an error.
mv dir1 dir2: in this command If dir2 does not exist, then dir1 is renamed dir2. If dir2 exists, the directory dir1 is moved within directory dir2.
options:
mv --backup[=CONTROL]: make a backup of each existing destination file.
mv -f, --force: do not prompt before overwriting.
mv -i, --interactive: prompt before overwriting.
mv -n, --no-clobber: do not overwrite an existing file.
If you specify more than one of -i, -f, -n,? only the final one takes effect.
Deleting Files and Directories:
The rm command removes (deletes) files and directories.
Options:
rm -f, --force: ignore nonexistent files and arguments, never prompt.
rm -i: prompt before every removal.
rm -I: prompt once before removing more than three files, or when removing recursively;? less intrusive than -i, while still giving protection against most mistakes.
rm -r, -R, --recursive: remove directories and their contents recursively.
rm -d, --dir: remove empty directories.
rmdir: Deletes an empty directory.
rmdir -p, --parents: remove DIRECTORY and its ancestors.
Editing Files:
nano, vim, emacs: Text editors for creating and editing files.
Wildcards
It is a shell feature that makes these commands so powerful. Since the shell uses filenames so much, it provides special characters to help you rapidly specify groups of filenames. These special characters are called wildcards. Wildcards allow you to select filenames based on patterns of characters.
Asterisk (*):
The asterisk matches zero or more characters in a filename or string (Matches any characters).
*.txt: Matches all files with a .txt extension
file*: Matches any filename starting with "file"
Question Mark (?):
The question mark matches exactly one character (?.txt: Matches files like a.txt, b.txt.).
square Brackets ([ ]):
Square brackets match any one of the enclosed characters. You can also specify a range of characters:
file[123].txt: Matches file1.txt, file2.txt, file3.txt.
file[a-c].txt: Matches filea.txt, fileb.txt, filec.txt.
[abc]*.txt: Matches any file starting with a, b, or c and ending with .txt.
[!] or [^]:
Matches any character that is not a member of the set characters.