DEV Community

Navaneethan Ghanti
Navaneethan Ghanti

Posted on

Complete Linux Essentials!

The Prompt

Open up the terminal, We see our prompt:

prompt

which includes your username@machinename, followed by a ~ and then a $, ~ represents home directory and $ represents that you are a regular user and not a super user.

Understanding the command structure

command -options arguments

  • -options: modifies the command behaviour, enables us to provide single or multiple options for a command.

  • arguments: many commands accepts arguments, these are the things that the command acts upon or uses.

Note: Commands are case sensitive.

Example:
sort -r file.txt, -r is an option(s) and file.txt is an argument.

Man Pages

Reference Manual page for commands, we can view the details of a command by running
man touch

Types of commands

  1. External commands: An executable program, usually stored in /bin, /usr/bin, or /usr/local/bin, Every command we run is a file on the system, its a compiled binary file.
    Note: you can see where your command file is stored by running which <commandname>

  2. Internal commands: A built in shell command, these are part of the shell (bash) and are not part of executable files, the shell executes them directly.

  3. Shell Functions: Shell functions are blocks of code that group commands together in a reusable way, they allow users to define their own commands within scripts.

  4. Alias: It is a shortcut for a command or a series of command, this can be defined in terminal session (temporary) or in .bashrc file (permanent).

Redirection

redirection

Redirection is the process of routing the standard input, standard output and standard error into a file or a program.

Each stream (input, output, error) gets its own numeric descriptor, standard input=0, standard output=1 and standard error=2.

  1. Redirecting the standard input: command < input.txt, contents of the file will act as the standard input and will be redirected to the command.

  2. Redirecting the standard output: if you run ls -l > files.txt, the contents will be stored in files.txt.

  3. Redirecting the standard error: cat unknownfile 2> error.txt, here, 2> is the standard error redirection operator, the contents will be stored in error.txt.

Redirecting standard output and standard error to the different VS same file using numeric descriptor.
ls docs > output.txt 2> output.txt AND ls docs > output.txt 2>&1

Absolute VS Relative Path

  • Absolute Path: This starts from ROOT(/) directory, /home/user1/Desktop/file.txt which has the complete path route.

  • Relative Path: Assuming we are in /home/user1/Desktop/ directory already, we can navigate to a folder called tests by running cd tests, this is because this is already in relation to our current working directory, and doesn't need an absolute reference.

Symbolic links

ln -s /your/long/path ~/shortcut

  • ln is the command to create links.
  • -s specifies it should be symbolic (or soft)
  • /your/long/path is the full directory path you want the shortcut for
  • ~/shortcut is the location and name for your symbolic link, which you can adjust as needed.

Once created, you would navigate to your long path directory by using your shortcut cd ~/shortcut
Symbolic links remain persistent accross sessions, In order to remove the shortcuts, rm ~/shortcut.

File and Directories Handling

Creating files and Folders

  • touch colors.txt will create colors.txt.

  • mkdir templates will create a folder called templates.

Copying, Moving, and Deleting

  • cp colors.txt /home/user1/Desktop/new/ will copy the colors.txt file to the specified path, and cp new/ /home/user1/Desktop/updated_new/, will copy entire folder contents into the updated_new folder.

  • mv colors.txt /home/user1/Desktop/new/ will move the colors.txt file to the specified path, and mv new/ /home/user1/Desktop/updated_new/, will move the entire folder contents into the updated_new folder.

  • rm colors.txt will delete colors.txt

  • rm -d <foldername>, deletes empty directories.

  • rm -r <foldername>, deletes folder and subfolder's in recursive mode. use -f option to delete everything by recursive force.

Listing Directories

ls displays the contents of a folder, -l option helps us to view it in long form, -la helps us to see the longform + hidden files and -lah option shows the human readable format.

More Useful Commands

  • pwd, prints the current working directory.

  • cd <dirname>, changes the current working directory to the mentioned directory.

  • cat <filename>, Displays the contents of a file on the terminal.

  • more <filename>, Views a file, page by page.

  • less <filename>, Similar to more, but allows backward scrolling.

  • nano <filename>, text editor that enables editing of a file.

  • whoami, shows the username of the current user.

Permissions

These are of three types: read, write and execute, the numeric descriptors for them are read=4, write=2 and execute=1, the file permissions for a file or folder can be viewed by listing the content of the diretories, ls -l, the first column displays the file permissions and the third column is user and fourth column is the group.

File Permissions

What is Owner, Group and World?

  • User(u): there can be multiple users on the system.

  • Group(g): Users can belong to a group which are given access to a particular files or folder.

  • World(o): Anyone apart from owner and not in group.

Altering Permissions

chmod <permissiontype>
for example, we can change the users permissions for a file or a folder by saying, chmod u+rw file.txt (change mode for user, add permissions read and write for the file named file.txt)

or we can do it numerically, giving all read, write and execute access for all of the roles by saying chmod 777 file.txt (change mode, give read, write and execute access for all of the roles).

Environment Variables

Shell maintains set of information during a shell session known as the environment, It is just a series of key value pairs separated by equals to sign, that define properties like:

  • Your home directory
  • Your working directory
  • The name of your shell
  • The name of the logged in user

Viewing the Environment

Type printenv in the terminal, to view the environment variables and their current values.

Parameter Expansion

You can print out an environment variable with an dollar sign using echo command, for example,

echo $USER, will print out the current user who is logged in.

Defining Variables

You can define a variable by saying variable=value, for example color=’purple’

Aliases

You can your own commands by using an alias keyword, for example,

alias ll = ‘ls -l’, now you can run ll instead of ls -l, remember setting alias will affect the shell session only, look on how to add it to global config file.

Startup Files

When we login, shell reads information from startup files, First, shell reads from global config file that affects the environment all of the users, Then, shell reads startup files for specific users.

The specific files the shell reads from depends on the type of session, login vs non-login session.

  • For Login Sessions:

/etc/profile : global config for all users.
~/.bash_profile : user’s personal config file.
~/.bash_login : read if bash_profile isn’t found.
~/.profile : used if previous two aren’t found.

  • For Non-Login Sessions:

etc/bash.bashrc : global config for all users

~/.bashrc :specific settings for each user. This is where we can define our own settings and configuration.

Shortcuts

  • ctrl-l : clears the commands in the terminal.
  • ctrl-a : move the cursor in the terminal to the beginning of the line.
  • ctrl-e : to move the cursor to the end of the line.
  • ctrl-f : to move the cursor forward by one character.
  • ctrl-b : to move the cursor backward by one character.
  • alt-f : to move the cursor forward by one word.
  • alt-b : to move the cursor backward by one word.
  • ctrl-k : to kill the text from the current cursor location until the end of the line
  • ctrl-u : to kill the text from the current cursor location until the beginning of the line.
  • ctrl-y : to retrieve the most recently killed text.
  • history : to see the list of commands we have previously entered, we can see the actual file at ~/.bash_history, run this command if you want to run a specific line from the history, !223 to run the 223rd line from the history.
  • ctrl-r : to enter “reverse-i-search”, as you start typing, bash will search the history and show you the best match.
  • ctrl-w : to kill the word from the current cursor position to the beginning of the word.

Conclusion

This blog defines the necessary linux concepts to get started and to build a foundation.

Thanks for reading, Any Suggestions or Feedback is Highly Appreciated!

Top comments (2)

Collapse
 
accursedgalaxy profile image
Accursed Galaxy | Robin

Great guide! I’ve been dual-booting for a while. Linux for work, Windows for gaming. The customization on Linux is unbeatable. At first, figuring out window managers, compositors, and dotfiles was confusing, but now I can’t imagine not having everything set up just how I like it.

One thing I‘d add: using symbolic links to navigate long directories!
Just run ln -s /your/long/path ~/shortcut, then cd ~/shortcut to jump there instantly.

Symlinks are awesome! They work across shells, scripts, and sessions—no extra setup needed. They act as real filesystem pointers, so they’re super reliable and keep everything neat!

Collapse
 
navv23 profile image
Navaneethan Ghanti

Superb info, thanks for sharing!