Working with the Command Line: Basic Linux Commands
The following article is part of a series of articles that provide tips and tricks for Linux newbies – or Desktop users that are not yet experienced with regard to certain topics. This series intends to complement the special edition #30 “Getting Started with Linux” based on openSUSE Leap, recently published by the Linux Magazine, with valuable additional information.
This article has been contributed by Lingshan Zhu, Software Developer Data Center Management, SUSE.
One of the biggest hurdles for a lot of people to adopt Linux is the fear to use the command line. True, the terminal can be a scary thing when you look at it for the first time. But it also comes with many advantages, and gives you much more control over your operating system and the actions you can take. And, if you just dare to engage with it, you will see it is way less scary than you thought. Thus, the goal of this article is to help you “survive” in the command line world, and to pilot your first steps (yes, also if you are a “light” Linux desktop user, you probably can learn something …), by giving you an overview of the most frequently used commands.
You can press the windows key on your keyboard. Type “Terminal” in the popup search box, then click the icon with a “Terminal” title. OK, that was easy, now you have a running terminal. Let’s see what you can do with it! And always remember that lower case and capital letters matter when typing commands.
To list directory contents, type ls in the terminal.
This command will return a list of all contents of a directory. Running ls /home will show all contents stored in your /home directory, while just running the command ls will show the contents under your current directory. There are two useful additional options for the ls command:
- -l: will display a long list with additional useful information like permissions, file size and last modification time.
- -a: will show hidden files or sub directories
The pwd command stands for “print working directory and will show you the name of your current working directory – that means “where you are” right now.
With the cd command, which stands for “change directory”, you can change your current working directory.
For example, if I am currently working in the directory /home/lszhu and I want to switch to /etc/, I just can change my current working directory using the command cd /etc.
The command rm is used for deleting files or directories. For example, rm ./foo.txt will delete the foo.txt file in your current directory. To delete directories, use the -r option, which stands for “recursive”. An example would be rm -r ./test. This will delete the directory named “test” with all files and sub directories in it.
The clear command will clear the terminal screen. This command can be useful if you worked for quite a while on one and the same terminal. You might want to clean it because your screen might seem “messy”. Just run clear – this lets you “wipe out” all words in the terminal.
You can move your files or directories to somewhere else with the command mv . For example, mv ./test /var will move the file test from the current working directory to the /var directory. mv can also be used to rename a file: mv ./test ./foo will rename the file originally named “test” to “foo”.
The command <i>mkdir<i> means “make directory” and allows for the creation of new directories.
Something like mkdir /home/lszhu/test_dir will create a new directory named test_dir under the /home/lszhu directory. There is also a useful option -p. This means “make parent directories as needed”. Imagine you have an empty directory /home/lszhu, then you want to create a directory called “workspace” and a directory “docs” under workspace. You can create the directories one by one in these steps:
mkdir workspace cd ./workspace mkdir docs
But using the -p option is more efficient: mkdir -p ./workspace/docs will create the “docs” directory, and as the parent directory workspace doesn’t yet exist, mkdir will create it as well within this same command.
cp is a command used to copy files or directories. Imagine you have a file named “scst.conf” in the current directory, and you want to copy it to the directory /var<i>. In this case, the cp command can help you by copying the file with cp ./scst.conf /var. If you want to copy a directory, the -r option can help you together with the cp command. As an example, cp -r ./test /var will copy the directory named test to /var with all files and sub directories in it. After that, you will see a directory /var/test, which has the exact same contents as the original test directory. You can also easily rename a directory or file when executing cp. For example, cp ./test /var/foo will first copy your test directory to /var, but it will then rename it to a new directory name foo under the /var directory.
su and sudo
The su command can help you to switch to superuser. The superuser is the system user who has all permissions to be able to perform administrative tasks. Some commands require superuser privileges. As an example, only the superuser can shut down the computer. When you use the command sudo as prefix, you can promote your privileges to superuser temporarily. As an example, you can type sudo poweroff to run the command to shut down the computer.
poweroff and reboot
The command poweroff shuts down the running system, while the command reboot cares for a reset of your system. Both commands require superuser privileges.
The man command displays the manual of the command you provided. As an example, man ls opens the manual of the command ls. To exit the manual, just press q for “quit”. You can use man for every command mentioned in this article to display the respective manual.
Of course not all of the many many cool Linux commands can be listed here – but these are the most frequently used ones and hopefully will be from help during your Linux journey.
Now keep in mind: no matter which command you might want to use, the man command might become your best friend in the Linux world. So, for example if you want to know how to compress or decompress files using tar, or how to login to another computer using ssh, just “man” these commands them!
Have a lot of fun!