Basic Command-line Knowledge for Beginners

The Terminal Basics

In the previous post, we mentioned UNIX is have a multi user system. Let’s talk some about what is this multi user. In the early of Unix was designed to run as a mainframe and terminals that only have keyboard and screen (no mouse, no graphic, no even color), connected to this mainframe remotely. There was no feature that this terminals have such that running any program independently from mainframe. Terminal were sending texts and receiving text.

which

We told that commands are executable program that we call binary. We apply which command to locate the executable file associated with the given command by searching it in the PATH (environment variable).

$ which ls
/bin/ls
$ pwd
/home/userName

cd

  • cd is an abbreviation of change directory.
cd ./../myNotes
  • .. is used for pass upper directory. Here, the command pass to the upper folder from the working directory, then it goes to inside of myNotes folder.
cd ~/Desktop

ls

  • ls is for listing files.
$ ls 
my-file.txt picture.jpg my-folder
$ ls my-folder
a-file
$ ls -l
drwxr-xr-x 4 user user 4096 Jun 12 11:19 my-folder
-rw-r--r-- 1 user user 10380 Jul 1 01:12 my-file.txt
-rw-r--r-- 1 user user 276659 Dec 21 2018 picture.jpg
$ ls -a
. .. .git my-folder my-file.txt picture.jpg
$ ls --helpUsage: ls [OPTION]... [FILE]...
List information about the FILEs (the current directory by default).
Sort entries alphabetically if none of -cftuvSUX nor --sort is specified.
Mandatory arguments to long options are mandatory for short options too.
-a, --all do not ignore entries starting with .
-A, --almost-all do not list implied . and ..
--author with -l, print the author of each file
...

man

This will print out all the information and use cases about the given program. You can also refer to the “man pages” (man is an abbreviation for manual) where you can find even more detailed information.

$ man ls

Relative and Absolute Path

Absolute path refers to the same location in a file system relative to the root directory, whereas a relative path points to a specific location in a file system relative to the current directory you are working on.

Manipulating Folders and Files

Now that we know how to navigate through the file-system, let’s see how we can create new files and folders.

Working with the Output

As you know, calling a command generally produces some output that is printed on our terminal screen. What if we want to save this output into a file so that we can just refer to it later instead of running the program? Easy:

$ ls -l > file-list.txt
$ cat file-list.txt
drwxr-xr-x 4 user user 4096 Jun 12 11:19 a-directory
-rw-r--r-- 1 user user 10380 Jul 1 01:12 my-file.txt
-rw-r--r-- 1 user user 276659 Dec 21 2018 picture.jpg

wc

Let’s say I want to find out how many files and folders in current directory. We can use the wc (word count) command with the -l parameter to find out how many lines a file have. We can combine wc and the file we just created:

$ wc -l file-list.txt
3
$ ls -l | wc -l
3

Conclusion

We learned some basic command line tools, how we can combine them and how we can find our way around in the terminal. This is good foundation to build your knowledge around the terminal. See you in my next post. ❤

References

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store