1. hello world in terminal
echo 'hello world!'
echo -n 'hello! world'
-n surpress the new line
ctrl+A go to the begginning of the line
ctrl+E go to the end of line
up/down key shows history of command
~, tilda is the variable for home directory
tab completion
clear command to clean the screen
man echo
to quit man page is to type q
Navigating man
- Page Down = Spacebar or the ‘Page Down’ Key
- Page Up = b or the ‘Page Up’ Key
- Line Down = j or the ‘Down Arrow’ Key
- Line Up = k or the ‘Up Arrow’ Key
- Top of Document = g
- Bottom of Document = G
- Quit = q
- Search = / to search forward [example /keyfile ] |or| ? to search backward [ ?keyfile ]
- Repeat Search = n to repeat the search forward and N to repeat the search in the opposite direction
- Help = h | Will give you the full summary of Less commands
2. Navigating the File System
ls
pwd
ls Documents //ls a directory
ls Documents Music // ls directories
ls /Users/js/Documents //using absolute path
ls Documents // same command as above
ls ~/Documents //same command as above
Case sensitive on Linux but not in Mac
For space use \
ls Some Directory // this will pick up two direcories
ls Some\ Direcotry
ls "Some Directory"
ls 'Some Directory'
ls -l // details list
ls -a //all the files including hidden files
ls -t //sort by time last modified
ls -t -l -a
ls -tla //or ls -alt etc
cd .. //one level up the directory
. is current directory
Mac finder integration in Mac. Drag a file on the teminal will show the absolute path of the file.
This will open the current directory in the Finder in Mac
open .
This will open a file with the default text editor.
open finename.txt
This will open a file with nameofApp.
open -a nameofApp nameofFile.txt
-a stand for application
-R reveal the file in the Finder
open -R filename.txt
Open a website in a default browser
open http://google.com
Displaying absolute path on the Finder
defaults write com.apple.finder _FXShowPosixPathInTitle -bool YES
killall Finder
3. Files, Links, and CRUD
Copying
Link: Linux and Unix cp command
touch finename
nano afile
mkdir dirname
touch adir/anotherfile
ls adir
cp afile afile.bak
If there is a superimportantfile, this will overwrite, so be careful.
cp afile superimportantfile //
cp adir seconddir // this will give an error
cp -r adir seconddir
More notes:
cp file1.txt newdir
Copies the file1.txt in the current directory to the newdir directory.
cp /home/public_html/mylog.txt /home/public_html/backup/mylog.bak
Copies the mylog.txt file in the public_html directory into the public_html/backup directory as mylog.bak. The files are identical however have different names.
cp *.txt newdir
Copy all files ending in .txt into the newdir directory.
cp -r /home/hope/files/* /home/hope/backup
Copies all the files, directories, and subdirectories in the files directory into the backup directory.
yes | cp /home/hope/files/* /home/hope/files2
Copies all the files and subdirectories in files into the files2 directory. If files with the same name exist or it's prompted to overwrite the file it answers yes.
Moving
mv afile adir // or mv afile ~/adir/
The following will move a file and rename it as afile-backup
mv afile.bak ~/adir/afile-backup
//renaming by mv command
mv afile secondfile
cd ~
touch file1 file2 file3 filejesse filejohn
Moving all files with 'file' name
mv file* adir/
More notes:
mv myfile.txt newdirectory/
Moves the file myfile.txt to the directory newdirectory.
mv myfile.txt ../
Moves the file myfile.txt back one directory (if available).
mv computer\ hope.txt computer_hope.txt
Moves (renames) the file "computer hope.txt" to computer_hope.txt. When working with a file or directory with a space you must escape that space with a backslash or surround the filename or directory with quotes.
Delete
rm command deletes files, be super careful
rm afile
In order to delete a directory you need to use -r
mkdir folderr
touch folderr/afile
ls folderr
rmdir folderr // error
rmdir -r folderr
Notes:
rm -rf directory/
Remove a directory, even if files exist in that directory.
rmdir mydir
Removes the directory mydir
Symbolic link/Soft link and Hard link
Go to the dir where you want to add a symlink and use ln -s to create the link.
cd /dir/where/youwant/toaddsymlink/ //link will be created in this dir
ln -s <destination folder/file name> <linkname>
In the video
touch afile
ln -s afile symlinforafile
ls
nano afile
// add hey there~
cat afile // to show the content hey there~
cat symlinkforafile // this will show hey there~ as well
But if you move a file, the link will break.
mv afile afile2
cat symlinkforafile // this is broken
A hard link won't break.
rm symlinkforafile
ls
ln afile2 hardlinkforafile2
cat afile2 // shows hey there~
mv afile2 afile3 //move the original
ls
cat hardlinforafile2 // this is not broken and shows hey there~
4. Finding Files
mkdir searchHere
cd searchHere
touch CaSeFiLe afile.doc athird newestfile aDir anotherfile bigfile smallfile afile anotherfile.doc fourth.txt
cd aDir
touch file1.txt file2.txt file3.txt
cd ..
Find by type
find . -type f //find name of dir, . means current dir and will find in aDir as well
Find by name
find . -name "newestfile" //this will find ./newestfile
find . -name "*.txt" //find recursively
//case incensitive -iname
find . -iname "*text"
Find by size
find . -size +2048 //more than 2048, 512bite which is more than 1MB
find . -size -2048 //find less than 1MB size
find by time of modification, creation or last accessed date
find . -mtime -1 //by modification time less than one day old
find . -mtime +1 // older than one day
find . -atime +1 // last accessed time more than one day
find . -atime -1 // accessed during the last day
find . -ctime -1 // created within one day
Combining them
find . -iname "*.txt" -or -iname "*.doc" // will find .txt or .doc
find . -iname "*.txt" -or -iname "*.doc" -and -mtime -1 // will find .txt or .doc and modified within one day.
Exclude a dir from search
find . -iname "*.txt"
find . -iname "*.txt" -or -iname "aDir" -prune // -prune means do not include in the seach but this will print aDir dir
find . -iname "*.txt" -print -or -iname "aDir" -prune// this won't print aDir
Search file for content
grep "Hello" afile// search word, file name
grep -i "Hello" afile //case incensitive match
grep -il "Hello" afile //-l will return the file name
grep -il "Hello" * //return all the file names
grep -ilr "Hello" afile //-r recursively search the current dir
Searching recursively and type of files won't work. You need to use 'find'.
grep -ilr "Hello" *.txt // this won't work
find . -name "*.txt" -exec grep -il "Hello" {} \;
-exec is passing -name "*.txt" to grep. {} is placehoder to put the outputs. and escape ;
5. Managing File Permissions
cd Documents
touch afile.txt
ls -l
-rw-r--r--
The first - is a regular file. d:directory, l:symbolic link
user(u), group(g), anyone(o) else. The above one, the user can read and write but not able to execute. Group and anyone else can only read.
chmod g+w afile.txt //giving write to group
ls -l
chmod o+w afile.txt //giving write to others
chmod a+x afile.txt //'a' stands for user, group and others. Giving executable to all.
Remove permissions by - instead of +. go is the same as og means group and others.
chmod og-x afile.txt
ls -l
Giving execution and taking writing permission to group.
chmod g+x-w afile.txt
ls -l
Giving group read, write and execution permission by using =
chmod g=rwx afile.txt
ls -l
Taking out all permissions
chmod g= afile.txt
Giving different permissions to different group.
chmod u=rw, g=,o=x afile.txt
ls -l
In absolute form using numbers. 4:read, 2:write, 1:execute
chmod 444 afile.txt
ls -l
chmod 744 afile.txt
chmod 700 afile.txt
chmod 644 afile.txt
6. Editing files
7. Piping, redirection and output
$ echo "hello world" > afile // single arrow will over-write.
$ cat afile
$ hello world // this is output.
echo "hello world" >> afile // this will append to afile.
redirecting an standard error
$ ls fakefile
$ ls: fakefile: No such file or directory
$ ls fakefile > log // this won't be any output in log file.
$ cat log
$ ls fakefile 2> log // 2 in front of the arrow is a standard error output.
$ cd fakedirectory 2>> log // this will append the error to log.
$ cat log
$ ls Docufakedir >> log 2>&1 // will append error to log
Outputing standard output and standard error.
$ ls Documents >> log 2>&1
8. Managing Processes on Mac OSX
9. SSH Keys for Password-Less Logins
10. Working With Remote Files: SCP, SFTP, and cURL
11. A Primer to Bash Scripting/Aliasing
12. Automated Background Processes
13. Configuring the Apache Web Server
14. Customizing the terminal
For zsh, read ,this](http://stevelosh.com/blog/2010/02/my-extravagant-zsh-prompt/) or Link: this article
Link: Bash Prompt Escape Sequences
Link: Bash Shell PS1: 10 Examples to Make Your Linux Prompt like Angelina Jolie
$ echo $PS1
$ export PS1="[\u]" // this will use the username
[username]
$ export PS1="[\W:\u]" // \W shows the base name of the current working dir
$ export PS1="[\u@\h:\W $]" //\h the hostname upto the first `.
These changes are temporaly. In order to make it permanent, open .bash_profile.
export PS1="\u@\h:\W $ "