Linux Commandline basics - file operations

in #linux7 years ago

Linux is one of most widely used operating system. It is everywhere – from servers and embedded devices, through desktops and mobile devices, ending on TVs and cars.
A lot of people who tried to use Linux tried to avoid using command line, as it seems to be daunting and hard to use. Fortunately it is not. Not only Linux command line is not hard, but also it’s very powerful, because it’s a scripting language, which can be used to automate things.

Linux and other Unix based operating systems have big variety shells available to use. Shell is a computer program which interprets and executes input commands. It acts as a bridge between user and operating system. I will cover only text-line shells in this miniseries of articles.

Linux has following shells at disposal (this is not a full list):

  1. Sh - Bourne's shell. It's in use since 1977, since Unix Version 7 release. Sh is also main shell for most Unix-like system.

  2. Bash - Bourne Again Shell. Bash were created as Sh replacement, extending it's capabilities. Bash can run sh commands. Comparing to sh it has:

    • Command history, which is stored in .bash_history in user's home directory.
    • Command auto-completion, command hinting.
    • It has built in arithmetics inside double parenthesis ((...)) and inside brackets $[...]
    • Upgraded input and output stream redirection.
    • Built-in regular-expression support. Syntax is similar to Perl: [[ str =~ expression ]]
  3. Csh - This shell has syntax based upon C programming language. It is not recommended for use, as Csh has limited scripting possibilities.

  4. Ksh - Korn's shell. Ksh is backwards compatible with sh, it includes many elements from csh and bash like command history.

  5. Zsh - Z Shell. Zsh is similar to ksh. It has builtin spell-checking/syntax-check, programmable command auto-completion, command history and many more.

I will use Bash as main shell in this miniseries.

So, after you boot up your system and after log-in you might see two different things - a graphical shell or text shell. If you are using any of desktop environment like GNOME, KDE, Unity, please start a terminal program, usually it's in tools folder on your menu-bar. (of course it all depends on DE you are using)

As I mentioned earlier I will focus on CLI (command-line interface) and Bash, also I'm going to work on Debian Sid After starting up your favourite terminal program or logging on to your system if you don't have any DE/XOrg installed you will see oh so daunting and hard command line omg i cant do anything help!!!!111one

Spoiler alert: if you need to see exact syntax of a command just add -h to command.

First thing you are seeing there is your prompt. It consist of your username, hostname of PC you are logged on to now and the directory you are now. It's formatted like this: <yourusername>@<yourpcname>:<actualdirectory>$

But wait ~ is not a directory! It's a weird looking trail! Wrong. ~ in Bash is used to denote your home directory. If you input (hint: to execute any command press Enter) echo ~ into your terminal, then you will see full path to your home directory which in my case is /home/jarric

Speaking of echo command, as the name suggests it "echoes" whatever you input as it first parameter. So if you would input echo steemit, it will print steemit to the output, if you would input echo $[2+2], the output will be 4. Why 4? As described in this article - Bash has built-in arithmetics

By now you know how to print stuff. I'd be awesome to know where are my files, right? ls command is going to help us. Let's input it without any parameter.

Hmm.... weird. Nothing is displayed. It's because there is nothing in my directory. Let's touch a file and see again output of ls command.

By now we have an empty text file. Let's add something to it. We can either use any text editor (which is too mainstream) or we could redirect output stream to file. Let's display our CPU information using lscpu command.

There is a lot of information about your CPU in the system. But wait... we have it printed out on screen. We want it to be inside our file. Add >> cpuinfo.txt to the command.

We have a second file. You may wonder what just happened. So, you have executed lscpu command and you have redirected stdout aka output stream to a file named cpuinfo.txt. By using >> we are appending data from stdout to the file.

You may wonder how to print file content. We will use a cat to do it. I'm not joking!
Input a cat to the prompt and file name of what we want to print to the screen. Believe me or not, this command is reading a file and printing out it's content to the screen.

It's all fun, but at some point we will have a problem. The amount of files we will create might become unmanageable. What we need in this case are directiories. But how do we create one? At our disposal there is a command named mkdir. Let's create a couple of directories.

Notice that directory has different color. It's used to make file listing a bit more readable.

Now we have a three directories and two files. Let's move those files to corresponding directories. We will be using mv command

We have successfully moved files to its directories.
To check contents of those directories we have two options. We can either use ls or change directory to the one which we want to check.
In first case we will need to append a path to the directory we want check on.

Easy? But what if we want to do something in the directory? The second option is for you. Let's use cd command.

You may see that our prompt has changed.

What if I want to go to parent directory? (remember, directories are tree alike structure) Use cd ... .. denotes parent directory.

Speaking of directories. Use pwd to know exact path to the directory you are in at the moment.

By now you know how to do basic interactions with Bash. In next article I will show you basics of Bash scripting.

For those who wonder why I didn't include any fancier parameters. That's a homework for you. Use -h parameter and man command to explore on your own.

Funfact: If you type man <command> they Linux will display very nifty manual to you.

Sort:  

@jarric, Contribution to open source project, I like you and upvote.

Thank you.