Linux – Basic Commands

echo

$ echo Hello there

cat

$ cat /etc/passwd 

displays the contents of the  /etc/passwd  system information file and then returns your shell prompt

$ cat file1 file2 …

cat prints the contents of file1, file2, and any other files that you specify (denoted by  …), and then exits.

  • Standard Input and Standard Output

Unix processes use I/O  streams  to read and write data.

Pressing CTRL-D on an empty line stops the current standard input entry from the terminal (and often terminates a program). Don’t confuse this with CTRL-C, which terminates a program regardless of its input or output.

Standard output is similar. The kernel gives each process a standard output stream where it can write its output.

The  cat  command always writes its output to the standard output.

There is a third standard I/O stream called standard error.

  • Basic  Commands

ls

The  ls  command lists the contents of a directory.

Use  ls  -l  for a detailed (long) listing and ls  -F  to display file type information

cp

In its simplest form, cp copies files. For example, to copy file1 to file2, enter this:

$ cp file1 file2

To copy a number of files to a directory (folder) named dir, try this instead:

$ cp file1 … fileN dir

mv

$ mv file1 file2

$ mv file1 … fileN dir

touch

The touch  command creates a file. If the file already exists,  touch  does not change it, but it does update the file’s modification time stamp printed with the  ls  -l  command

$ touch file

rm

To delete (remove) a file, use rm.

$ rm file

echo

$ echo Hello again

  • Navigating  Directories

cd

The  cd command changes the shell’s current working directory:

$ cd dir

mkdir

The mkdir command creates a new directory dir:

$ mkdir dir

rmdir

The rmdir command removes the directory dir:

$ rmdir dir

If dir isn’t empty, this command fails. You can use rm -rf dir to delete a directory and its contents. Don’t use the  -rf  flags with globs such as a star (*). And above all, always double-check your command before you run it

  • Shell Globbing (Wildcards)

The shell can match simple patterns to file and directory names, a process known as globbing.

he following comm and prints a list of files in the current directory:

$ echo *

  • at* expands to all filenames that start with at.
    • *at expands to all filenames that end with at.
    • *at* expands to all filenames that contain at.

the question mark (?), instructs the shell to match exactly one arbitrary character. For example, b?at matches boat and brat.

  • Intermediate  Commands

grep

The  grep  command prints the lines from a file or input stream that match an expression

$ grep root /etc/passwd

print the lines in the /etc/passwd file that contain the text root,

less

The less command comes in handy when a file is really big or when a command’s output is long and scrolls off the top of the screen. To quit, type q

$ grep ie /usr/share/dict/words | less

pwd

The  pwd  (print working directory) program simply outputs the name  of the current working

directory

pwd -P for full get the true full path of the current working directory

diff

$ diff file1 file2

To see the differences between two text files, use diff

file

If you see a file and are unsure of its format, try using the file command to see if the system can guess:

$ file file

find and locate

Run find to find file in dir:

$ find dir -name file -print

head and tail

To quickly view a portion of a file or stream of data, use the head and tail commands

head /etc/passwd  shows the first 10 lines of the password file, and 

tail /etc/passwd  shows the last 10 lines.

To change the number of lines to display, use the -n  option, where n  is the number of lines you want to see (for example, head -5 /etc/passwd). To print lines starting at line n, use tail +n

sort

The sort command quickly puts the lines of a text file in alphanumeric order. If the file’s lines start with numbers and you want to sort in numerical order, use the -n option. The -r option reverses the order of the sort.

Reference: How Linux Works: What Every Superuser Should Know by Brian Ward


Discover more from Embedded for All

Subscribe to get the latest posts sent to your email.

Leave a comment

Discover more from Embedded for All

Subscribe now to keep reading and get access to the full archive.

Continue reading

Design a site like this with WordPress.com
Get started