Basic Linux Commands

Introduction: What is Linux?

  • An operating system
  • Developed by Linus Torvalds in the 1990s
  • Comes with a Command Line Interpreter (CLI)
  • GUIs (Window systems) are now available

Introduction: Why Unix/Linux?

  • Linux is free
  • It’s fully customizable
  • It’s stable (i.e. it almost never crashes)
  • These characteristics make it an ideal OS for programmers and scientists

What exactly is a CLI or “shell”?

  • The shell interprets commands the user types and manages their execution
  • The most popular shells are: tcsh, csh, korn, and bash
  • For this tutorial, we are using bash

Help!

Whenever you need help with a command type man followed by the command name

FILE SYSTEM COMMANDS

pwd

To find your current path use “pwd”

Syntax: pwd

cd

To change to a specific directory use cd command

Syntax: cd <filename>

  • “~” is the location of your home directory
  • “..” is the location of the directory below current one

ls

To list the files in the current directory use “ls”

Syntax: ls

ls has many options:

  • -l long list (displays lots of info)
  • -t sort by modification time
  • -S sort by size
  • -h list file sizes in human readable format
  • -r reverse the order

Type man ls for more options

Options can be combined:

  • Command: ls -ltr

List files by time in reverse order with long listing

Wildcards

General Syntax: *

* can be used as a wildcard in Unix/Linux

PERMISSIONS

File permissions

  • Each file in Unix/Linux has an associated permission level
  • This allows the user to prevent others from reading/writing/executing their files or directories
  • Use “ls -l filename” to find the permission level of that file

Permission levels

  • “r” means “read only” permission
  • “w” means “write” permission
  • “x” means “execute” permission

In case of directory, “x” grants permission to list directory contents

chmod

If you own the file, you can change its permissions with “chmod”

Syntax: chmod [user/group/others/all]+[permission] [file(s)]

mkdir

To create a new directory, use the mkdir command

Syntax: mkdir <dirname>

rmdir

To remove an empty directory, use "rmdir".

Syntax: rmdir <dirname>

Displaying a file

Various ways to display a file in Unix

  • cat
  • less
  • head
  • tail

cat

Dumps an entire file to standard output

Syntax: cat <filename>

less

“less” displays a file, allowing forward/backward movement within it

Syntax: less <filename>

  • return scrolls forward one line, space one page
  • y scrolls back one line, b one page
  • use “/” to search for a string
  • Press q to quit

head

“head” displays the top part of a file

Syntax: head <filename>

  • return scrolls forward one line, space one page
  • y scrolls back one line, b one page
  • use “/” to search for a string
  • Press q to quit

tail

Same as head, but shows the last lines

Syntax: tail <filename>

File Commands

Common File Commands

  • Copying a file: cp
  • Move or rename a file: mv
  • Remove a file: rm

cp

Copies a file from source to destination

Syntax: cp <source> <destination>

mv

To move a file to a different location use “mv”

Syntax: command <source> <destination>

  • mv can also be used to rename a file

rm

To remove a file use “rm”

Syntax: rm <filename>

  • To remove a file recursively, type: rm -r
  • Used to remove all files and directories

Be very careful, deletions are permanent in Unix/Linux

wc

To count the characters, words, and lines in a file use “wc”

Syntax: wc <filename>

The outputs are lines, words and characters.

Web Resources

let's go on