Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
319 views

Linux Overview (Commands)

The document provides an overview of the Linux file system and common commands used for file navigation and manipulation. It discusses: 1) The basic tree structure of the Linux file system with / as the root directory and files/folders added by appending them to this structure. 2) Commands for exploring the file system like ls, pwd, cd, cat, and less. 3) Commands for creating/removing directories like mkdir, rmdir, and removing files with rm. 4) Commands for copying/moving files and directories like cp, mv, and deleting files with rm. 5) The grep command for searching files and editing files with nano.

Uploaded by

fopata
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
319 views

Linux Overview (Commands)

The document provides an overview of the Linux file system and common commands used for file navigation and manipulation. It discusses: 1) The basic tree structure of the Linux file system with / as the root directory and files/folders added by appending them to this structure. 2) Commands for exploring the file system like ls, pwd, cd, cat, and less. 3) Commands for creating/removing directories like mkdir, rmdir, and removing files with rm. 4) Commands for copying/moving files and directories like cp, mv, and deleting files with rm. 5) The grep command for searching files and editing files with nano.

Uploaded by

fopata
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 9

Linux overview

Before we kick things off, below is a brief intro into the Linux file system.

The file system controls how data is stored and retrieved in a computer. All files and
folders in a Linux system are part of a bigger tree-like structure rooted at /. Files and
folders are added to the file system by appending them to this tree structure, and
deleted by removing them. All file names are case sensitive. When working with files
and directories on the command line, special characters, like space and brackets, have
to be escaped using a backslash.

Here are a few helpful navigation commands to help you explore the Linux file system:

You can check out the contents of the current directory using the ls command.

ls
You can view more details about the files, like ownership and permissions, by adding
the flag -l to the ls command.

ls -l
You can see hidden files in the current directory by passing flag a to the ls command.

ls -a
You can find out where you are in relation to the rest of the file system using
the pwd command.

pwd
You can navigate to different directories using the cd command.

cd /path/to/other/directory
You can check out the contents of a file using the cat command.

cat /path/to/file/file_name
For large input files, the less commands allows movement within the files. The syntax is
similar to that of the cat command, but you can move.

Reading large file using less

less /path/to/file/file_name
The command will provide you with a scrollable view of the content within the file, up to
the end of the file content. Scroll down using "Enter", and exit the view by pressing "q".

Creating directories (folders)


Directories (folders) in Linux are created using the mkdir command. The command
takes the directory name as the argument.

Note: The example commands are for reference only.


Example 1

mkdir dir_name
Multiple directories can be supplied as arguments, and mkdir will create all of them.

Example 2

mkdir dir1 dir2 dir3

Parameters

mkdir can take three options:

 -p: allow mkdir to create parent directories if they don't exist


 -m: (mode) used to set permissions of directories during creation
 -v: run command in verbose mode

Removing empty directories

To remove empty directories, use the rmdir command. The name of the directory to be


removed is passed as an argument.

Note: The example commands are for reference only.


Example 1

rmdir dir_name
Multiple directory names can be passed as arguments, and rmdir will remove all of
them.
Example 2

rmdir dir1 dir2 dir3 dir4


Head's up: rmdir only removes empty directories. To remove a non-empty directory, the
command rm, discussed later in this course, is used.

Options

rmdir takes only one option, which tells it to remove parent directories if they're also
empty.

 -p: remove parent directories, if they're also empty


Creating files

By default, the touch command is used to change the modification and access times of
a file. If the file doesn't exist, the touch command is used to create a file with default
permissions.

Let's take a look at an example of how to use the touch command. In the current
directory, we can create an empty file called "empty_file":

touch empty_file
The touch command can take the c option to prevent a new file from being created.

Options

 -c: do not create file if it doesn't exist


Copying, moving and deleting files and
directories (folders)
cp

The cp command is used to make a copy of one or more directories or files. The
command takes at least one source name and one target name. If the target is a file,
then the source must also be a file. A copy of the source will be made with the new
name supplied in target. If the target name isn't specified, a copy of source will be made
in the target directory under the same name. If a file with the target name already exists
in the target directory, it'll be replaced. If the target is an existing directory, then all
sources (one or more) will be copied into the target directory. If the target is a directory
that doesn't exist, then the source must also be a directory. A copy of the directory and
its contents will be made in target under the same name.

Note: The example commands are for reference only.

Example 1

Copy the file "source_file" in the directory /home/user/ to the directory "duplicates" as


"target_file".

cp /home/user/source_file /home/user/duplicates/target_file

The duplicates directory now contains a copy of the original file.

mv
The move command is used to move one or more files or directories into a different
location, or rename them to a new name. You're required to pass at least one source
and target file names or directories. The mv command follows the rules for existing or
non-existing directories or files, as does cp.

Example 2

Move the file "source_file" in /home/user/ to the directory "moved_files" and give it the
name "target_name".

mv /home/user/source_file /home/user/moved_files/target_file

The original directory doesn't contain the file now. It's been moved to the new directory
"moved_files".

rm

The rm command is used to remove one or more files. You need to supply at least one
argument to remove.

Example 3

We can remove the duplicate file we created in the directory "duplicates" using rm

rm /home/user/duplicates/target_file

In the directory /home/user/Movies, there's a folder called "Europe Pictures". We'll move


this folder into the correct directory for pictures: /home/user/Pictures. Note the use of
the backslash "\" to escape the space between "Europe" and "Pictures" in the directory
name, "Europe Pictures".

mv /home/user/Movies/Europe\ Pictures /home/user/Pictures

You can also use a "dot" to copy or move files to the current directory. In the
directory /home/user/Images, we can move the file "Vacation.JPG" into the Pictures
directory. To do that, we change into the Pictures directory, then add a "dot" to
the mv command as the target.
cd /home/user/Pictures

mv /home/user/Images/Vacation.JPG .

To remove a directory with content, the rm command is used instead of rmdir. The option -r tells
the command to remove the directory, along with its content recursively.

rm -r non_empty_dir

Searching in files
grep

grep is a super powerful Linux command used to search through files for the occurrence
of a string of characters that matches a specified pattern. We can use the command in
combination with a bunch of different options and flags for efficient searching.

Options and flags

 -r: search recursively


 -w: match the whole word
 -n: only in line number
 -e: match pattern
 --include and --exclude: include and exclude files in the search
 --include-dir and --exclude-dir: include or exclude directories in the search
Editing files

Lots of Linux distributions come with pre-installed text editors. The most popular ones
are vi and nano, which will be included in nearly every distribution. Other text editors, like
Emacs and Gedit, might also be present. In this lab, we'll modify files using the Nano
editor.

You can use the nano command to open the Nano editor and modify an existing file, or
create a new one. To edit an existing file, we'll first start with opening it.

Note: The example commands are for reference only.


Example:

nano /path/to/existing/file
The command will open the file in the terminal and display the current file contents. To
modify, you can edit the content in the terminal, just like a normal editor. The editor is
managed using various shortcuts.

To save modifications to the file, use Ctrl+O-:

CTRL-O
Once editing is done, we can close and exit the program using Ctrl+X

CTRL-X
NB: At any point in using the editor, you can get help using Ctrl+G

CTRL-G
To exit help mode, use Ctrl+X

CTRL-X
Alright, now let's practice how to edit files using nano.

In the current directory, create an empty file called editor_test.txt

touch editor_test.txt

Open the file with the Nano editor.

nano editor_test.txt
Add content to the file. (In this case, we add five lines, each separated by an empty line.)

Save the file by hitting Ctrl+O

CTRL-O
You'll need to confirm the file that you want to write the content to by hitting Enter. After
this, exit the program by hitting Ctrl+X

CTRL-X
That's it! You've successfully created and modified a file.s

You might also like