LanRaccoon

Practical tech advice

Linux command line – things a programmer can learn in a day

If you’re doing software development, chances are you had to do with Linux. It’s nearly impossible to go around it. Even Windows has linux now. Although getting good with Linux it’s more than I can do in a single blog post, I find that it’s quite easy to get working knowledge. Just enough to make you dangerous. 

Go to a folder:

cd some_folder_name 

Go to the parent folder:

cd ..

Show the current folder (where you are):

pwd

Finding things

List the contents of the folder:

ls

List the contents of the folder with some additional details (including file size):

ls -alh 

Show the size of a folder:

du -h folder_name

Show the total disk usage:

df -h 

Looking for a file by name:

find /path/to/folder -iname something_to_find

There are several options to using find that expand the use cases a lot. You can probably find what you search for on the manual page.

Modifying contents

Create  a file:

touch new_file_name

Delete a file:

rm file_name

Create a folder:

mkdir folder_name

Delete a folder: 

rm -r folder_name

Permissions

Change file permissions:

chmod 644 file_name

You can use the chmod -R to change the permissions of everything in a folder (recursively)

Change the owner of a  file: 

chown user:user_group file_name

You can use the chown -R to change the owner recursively for a folder

Processes

Show all processes running on the system (including process IDs):

ps aux

You can also filter to find a process by name

  ps aux | grep “nodejs”

Stop a process:

kill -9 proces_id 

Resources

Show available resources on a system:

top

Some additional features top has: 

  • You can switch between different views by typing M
  • You can sort processes by memory usage by typing Shift + M
  • You can sort processes by CPU usage by typing Shift + P 
  • Use ‘q’ to quit top

Show open network connections:

netstat -ano 

You can filter for an opened port using grep:

netstat -ano | grep 5432

Looking into files

See the contents of a file:

cat filename

For a more interactive experience use:

less filename 

More extra features available for less:

  • You can go line by line using “Enter”
  • You can go screen by screen using “Space”
  • You can search the file by typing “/” and then use regular expression for pattern matching
    • You can navigate between matches using “n” (move to the next match) and “Shift + n” move to previous match.

Get the number of words in a file:

wc file_name

Get the number of lines in a file:

wc -l file_name

See the start of a file:

head -n 100 file_name

See the end of a file:

tail -n 100 file_name

Explanation on parameters:

  • 100 is, you guessed it, the number of lines to display
  • You can use either tail with -f to monitor a file and display contents as they are added (for example when monitoring a log file to see what is happening) 

Services

*You might need to run some of these commands with sudo to get access to the service.

Show all running services:

service --status-all

Show the status of a service:

service service_name status

Stop a service:

service service_name stop

Start a service:

service service_name start

Full commands

Linux terminal has quite powerful tools and via piping and output redirects you can create useful effects. 

Show all established connections:

netstat -ano | grep ‘ESTABLISHED’

Show the most memory intensive processes on the system:

ps aux –sort -rss | head -n 11

11 because there will be header line and one line for each of the 10 processes.

Show last logs for a given time:

cat log_file_name | grep -A 3 ‘20:01:15’ | tail -n 20

-A is used here to include the following two lines after the match (just in case logs span more than one line) 

Final thoughts

This is just scratching the surface of that can be done inside a terminal. There is so much more lurking below the surface. I suggest looking at the man page for the command you are interested, you might find additional information there. You can find the manual page for a command by running

man command

Or just go to Google. 

Further reading

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Scroll to top