It's time to get acquainted with your command line
and learn the commands that you'll actually need to use
            
          
Doug McLiroy - inventor of Unix Pipes
              
            text-way to interact with PC
  the command line is a bit intimidating
  text-only way of browsing and interacting with your computer
  no mouse, no icons, nothing but text
  DOS prompt > boot PC > C:>
  That’s an example of the command line!
  window into your computer
              
            
You'll become very familiar with those commands
              
            Command Line Bash Bourne-Again SHell Unix Shell
text-based shell for controlling the computer
program itself that
gives us a window into our OS
and lets us run commands
navigate your file system
just like you would by double clicking on folder icons in the Finder
              
            
Romans-Macbook-Pro:~ somename$
<computer-name>:<current-directory> <username>$
<computer-name>:<current-directory> <superuser>#
              
            
            
              
            
              
            
[me@linuxbox me]$
[me@linuxbox me]$ kdkjflajfks
bash: kdkjflajfks: command not found
              
            
            
команда1> команда2>
команда1 | команда2
              
            
            first shell in Unix 1971 y
              
            
Unix 7, 1977
AT&T
/bin/sh
скриптова мова
              
            
#!/bin/bash
echo Hello World!
              
            
            
              
            
over bash
1983
Devid Korn AT&T
              
            
over bash
Can Greag AT&T
autocompl (tab)
spellchecking
events
last update 2012
              
            
hierarchical directory structure
              
            
/ the root directory /boot Linux kernel and boot loader files /etc the configuration files for the system /bin, /usr/bin contain most of the programs for the system /sbin, /usr/sbin programs for system administration /usr variety of things that support user applications /var contains files that change as the system is running (/var/log) /home users keep their personal work /tmp for temporary files
$ command -options arguments
              
              
$ echo "hello world"<enter>
$ ls -la
$ cd My\ Folder\ Name
              
              Documents != documents
            
          
[me@linuxbox me]$ pwd
/home/me
              
            
          
[me@linuxbox me]$ ls
Desktop     Xrootenv.0    linuxcmd
GNUstep     bin           nedit.rpm
GUILG00.GZ  hitni123.jpg  nsmail
              
            
          
$ ls
$ ls /bin
$ ls -l
$ ls -l /etc /bin
$ ls -la ..
              
            
          
[me@linuxbox me]$ cd /usr/bin
[me@linuxbox bin]$ pwd
/usr/bin [me@linuxbox bin]$ ls
[                     lwp-request
2to3                  lwp-rget
2to3-2.6              lxterm
a2p                   lz
aalib-config          lzcat
aconnect              lzma
acpi_fakekey          lzmadec
acpi_listen           lzmainfo
add-apt-repository    m17n-db
addpart               magnifier
and many more...
              
            
          
[me@linuxbox me]$ file <name_of_file>
              
            
          
$ pwd
$ hostname
$ mkdir
$ cd
$ ls
$ rmdir
$ cp
$ mv
$ cat
$ find
$ grep
$ man
$ env
$ echo
$ exit
$ sudo
$ chmod / chown
              
            
          
$ man ls
              
              
          
$ hostname
Zeds-MacBook-Pro.local
              
            
          
$ pwd      # Your Current Directory
/Users/zedshaw
$ ls       # View Directory Contents
$ ls <directory-name>
$ ls -la 
$ ls -la .
$ ls -la *.txt
$ open .   # Open a file/directory
              
            
          * everything g* everything that begins with character "g" b*.txt begins with "b", and ends with ".txt" Data??? begins with the characters "Data" followed by exactly 3 more characters [abc]* begins with "a" or "b" or "c" followed by any other characters
~/Documents/some_folder/some_other_folder/some_file.tx
. = root
.. = up
~ = home directory
              
            
          
[me@linuxbox me]$ cp file1 file2
[me@linuxbox me]$ cp file... directory
              
            
          
[me@linuxbox me]$ mv filename1 filename2
[me@linuxbox me]$ mv file... directory
              
            
          
[me@linuxbox me]$ rm file...
[me@linuxbox me]$ rm -r directory...
Be careful with rm!
[me@linuxbox me]$ rm -rf /
              
            
          
[me@linuxbox me]$ mkdir directory...
              
            
          
$ cp *.txt text_files
$ mv my_dir ../*.bak my_new_dir
$ rm *~
$ ls **/*.jpg > jpg-dir.txt
$ unexisted-command 2> /dev/null
              
            
          
$ pwd
$ cd <folder-name>
$ cd ..
$ cd ~
              
            
          
$ mkdir some/folder
$ mkdir -p some/folder/
$ touch test.txt
$ open test.txt
$ cat test.txt
$ mv test.txt test2.txt
$ rm test.txt
$ cp test.txt
              
            
          
$ alias ll='ls -la'
$ alias rmi='rm -i'
$ alias cia='. /home/sydney/env/cia.sh'
              
            
          
$ history
$ !!       # recall the latest command
$ !1003    # recall the latest command by its number
$ !cat     # recall the latest command matching a starting string
              
            
          
$ find . -name test.js
$ find /etc -name '*.txt'
$ find . -name '*.xml'
$ find . -not -name '*.xml' -maxdepth
$ find . -type f
$ find . -mtime -1
$ find . -mmin -15
$ find . -size -1k
              
            
          Global Regular Expression Print g/re/p for finding text inside files
$ grep <string> <file or directory>
$ grep 'some text' file.js
$ grep something file.js
$ grep -r 'Dao[v1|v2]' src.js
$ grep -i
              
            
          
condition { actions }
$ awk 'END { print NR }' server.log
              
            
          
# Display information about command type
# (executable, built-in shell, function, alias)
$ type cp
cp is /bin/cp
# Locate a command
$ which ls
/bin/ls
# Display reference page for shell builtin
$ help -m cd
...
# Display an on-line command reference
$ man ls
...
              
            
          
$ ls -l
total 72
# Permissions  Links  Owner  Group  Size  Modification  File Name
drwxr-xr-x   2 root root  4096 Oct  5 09:31 bin
drwxr-xr-x   3 root root  4096 Oct  9 21:47 boot
drwxr-xr-x   1 root root     0 Jan  1  1970 dev
...
              
            
            set of file attributes you'll run into, though, is permissions
Every file and directory in the system has an owner,
belongs to a group
and has a set of permissions
              
            
:type : owner : group : restofworld:
:d    : rwx   : r-x   : r-x
                
              
$ whoami
joe
$ id -G -n
                
              
$? = from 0 to 255 zero = success
$ echo "this works fine"
this works fine
$ echo $?
0
$ hhhhhh
bash: hhhhhh: command not found
$ echo $?
127
              
            
            When you type commands
print an error message
              
            126 = permission denied
$ printenv
              
            
          
# In ~/.bash_profile
export MYSQL_DIR=/usr/local/mysql/bin
PATH=$MYSQL_DIR:$PATH
              
            
          
$ which open
              
            
          
read each time when bash shell is started
$ command1 && command2 [&& command3]...
$ command1 || command2 || command3 || ...
$ command1 && command2 || command3
              
            
            
$ ls > my-file-list
$ ls >> my-file-list
$ sort < my-file-list
$ ls *.bak > listfile
$ ls /nosuchplace > /dev/null
ls: /nosuchplace: No such file or directory
$ ls /nosuchplace 2>/tmp/errors
$
              
            
            Each program can open a lot of files
each has a number called a file descriptor
first three numbers are always reserved
/dev/null /dev/zero /dev/full
              
            
$ sort < file_list.txt > sorted_file_list.txt
              
            
            
$ cat books | grep "a"
Carroll, Lewis:Through the Looking-Glass
Shakespeare, William:Hamlet
$ ls | wc -l
$ ls | xargs wc -l
$ ls -lt | head
$ du | sort -nr
              
            
            Filters
Pipes let programs work together by connecting the output from one to be the input for another
              
            
$ find . -name '*.js' | xargs wc -l | sort
              
            
            
#!/bin/bash
$ whereis bash
              
            
          
#!/bin/bash
parametr1=$1
script_name=$0
echo "welcome to $script_name"
echo 'welcome to $script_name'
exit 0
              
              
user@desktop:~$ ./test1.sh param1
              
            
            no variable types
              
            
if
  <command>
then
  <commands-if-successful>
else
  <commands-if-failed>
fi
              
            
            
              
            
#!/bin/bash
source=$1
dest=$2
if [[ "$source" -eq "$dest" ]] then
  echo "some1"
  exit 1
else
  echo "some2"
  exit 2
endif
              
              
user@desktop:~$ ./test2.sh 1 2
              
            
            no variable types
              
            
$ sudo <command>
              
              
            Some files on the computer are protected
who has the power to do anything
administrator password
              
            
[me@linuxbox me]$ su
Password:
[root@linuxbox me]# chown you some_file
[root@linuxbox me]# exit
[me@linuxbox me]$