If you are working with linux based operating system you will realize that most important skill to learn first is to navigate around file system. When you are logged into a linux server with your user and password, you will land in home directory of the logged in user.
How to login inside Linux/Ubuntu?
Let say that you are a developer and given access to linux server. You want to first log into to server via SSH. SSH (Secure Socket Shell) is a protocol used by users to connect to remote server.
During SSH session, any commands that you type into your local terminal are sent through an encrypted SSH tunnel and executed on your server. You must have ssh client on your local machine to log on to remote server.
Let say that you want to login to remote linux server try following command to securly log into linux server hosted remotely.
# login using domain name ssh user@serverdomain.com # login using ip address ssh user@ip-address
There are two methods you can use to login to your linux server:
- using domain name i.e. google.com
- using ip address of the server i.e. 127.0.0.1
How to check which directory you are in?
Once you login onto your server the first command that you need to know is pwd. When you run this command on your terminal window it will show you which directory you are currently in.
# login to your server ssh user@ip-address # check the current directory # output: /home/user pwd
How to list all files in current directory?
Now, that you know how to check which directory you are in. Next, important comamnd is to know what is inside a perticular directory.
Syntax: ls <options> <directory-path>
Let say that you want to know what files and folders are within a specific directory you can use following command:
# syntax: ls <options> <directory-path> # list all directory and files excluding hidden files ls # list all directory and files including hidden files ls -a # list the contents of the directory with it's subdirectories excluding hidden files ls * # list all files and directories with their corresponding subdirectories down excluding hidden files ls -R # list files or directories with their sizes ls -s # ist the contents of the directory in a table format with columns ls -l # show only text files in current directory ls *.txt
Above are some of the basic usage of ls command. However, if you want to explore more advanced usage of ls command here are some examples:
List files and sort by date and time in Linux/Ubuntu?
To list all the file and folders sort by date and time you can use following command:
# list files and folders in decending order i.e. recently created first ls -lt # list files and folders in ascending order i.e. reverse order ls -ltr
List files by size and sort files by size in Linux/Ubuntu?
If you like to list all files and folders within a specific directory and sort them by their size you can use following linux command:
# list all files in table format, add new size column # sort file/folders by their size in descending order (biggest to smallest) ls -lhS # list all files in table format, add new size column # sort file/folders by their size in ascending order (smallest to biggest) ls -lhSr
You can see how powerful these commands are. You can combine multiple commands and can perform different operations effectively in linux. If you are a beginner with linux learn command with different options and sharpen your skills by combining different comamnds.
A developer must have some linux skills to be comfortable with linux based o/s. Trust me you need to work with linux based o/s once you are getting pro develper.
Try some of the following excercises by your self and see how good you become with ls command:
- list all directories only and add this list to a new file called directories.txt
- show only pdf files in given directory
- show list of files along with their size in table format i.e. file size
Moving on, we learned:
- how to login to remote server using ssh command
- how to show current directory name using pwd command
- how to list files and directories in any directory using ls command
How to change directory in linux/ubuntu?
if you want to change a directory in linux o/s following is a command syntax:
cd <directory>
Before we learn some examples it is important to learn following concept:
- in linux dot (.) refers to current directory
- double dots (..) refers to previous directory
- slash (/) refers to root directory
Examples:
# go one directory back from current directory cd .. # go to two directory back cd ../.. # go to root directory cd / # go to demo user's home directory cd /home/demo
Alright, so far in this tutorial we talked about navigating files/folders and list directory/files. In next tutorial we will learn about different types of view operations. Please do like and share my tutorials if you believe you learned something useful by reading my tutorials.