Common OSX/Unix Commands

Common OS X/Unix Commands

Navigation Commands

1. cd: change directory

cd – : go back to previous directory

cd .. : go up to the parent of the current directory

cd ../..: go up two levels, to the parent’s parent directory

cd /: go to the top of the boot volume

cd /Users

cd ~ : go to home directory

cd ~/Documents

2. pwd: print working directory, the current path

3. ls: just list the names of the files in the current directory

ls –l: (long) list the files with their characteristics

ls –lo : list the files with their flags

ls –a: list all files in the current directory

ls –F: list filenames with a special character at the end that tells you what kind of file

it is

ls *.jpg: list the names of all files with names ending in ‘.jpg’

ls a*: list the names of all files start with ‘a’

ls *arr*: list the names of all files with names containing ‘arr’

File & Folder Manipulation Commands

1. cp: copy a file or directory

cp foo bar: copy a file named foo in current directory, and name the copy bar

cp foo ~/Documents: copy foo to Documents directory

cp foo ~/Documents/bar: copy foo to Documents, and name the copy bar

cp *.jpg ~/Documents: copy all jpg images to Documents

cp –R Documents “Documents backup”: back Documents, and name it Documents backup

sudo cp –Rp /Users “/Users backup”: copy entire Users directory preserving as much

as possible of the files information

2. mv: move of rename a file or folder

mv foo bar: rename foo to bar

mv foo ~/Documents: move foo to Documents directory

mv foo ~/Documents/bar: move foo to Documents and rename it to bar

mv *.jpg ~/Documents: move all jpg images to Documents

rm: remove a file /folder

rm foo: delete foo

rm a*: delete all files name start with a

rm *.jpg: delete all jpg images

rm –R temp: delete temp folder and all its contents

3. mkdir: make directory

mkdir foo: create a new directory foo

4. rmdir: remove an empty directory

5. ditto: copy a directory, preserving many characteristics of the enclosed files

6. chmod: change protection mode on files and folders, deal with complex access right with files

7. chflags: change a file or folder’s flags: arch, opaque, nodump, sappnd, schg, uappnd, uchg

chflags uchg foo: lock file or folder named foo against changes

chflags uappnd foo: make the file ‘foo’ append-only

chflags –R nouchg ~/Documents: unlock Documents and everything in it

8. chown: change the owner or group of a file or folder, must be root

sudo chown eva /Users/Shared/meeting-notes.txt: switch to root and assign eva as

the owner of meeting-notes.txt

sudo chown –R eva:staff /Users/Shared/temp: assign eva as the owner and staff as

the group for folder temp

9. chgrp: change the group of file or folder

chgrp staff /Users/Shared/meeting-notes.txt: Assign staff as the group of the file

chgrp –R staff /Users/temp: assign staff as the group of temp

Working with Text files

1. more and less: display the contents of a text file

2. tail: display the last few lines of a text file, useful for examining logs

tail -100 /var/log/system.log | more: use more to display last 100 lines of system.log

tail –f /var/log/system.log: print the last screenful of entries from system.log then

follow changes to file

3. vi, emacs, nano: other text editors, see man accordingly

4. tr ‘r’ ‘n’ <macfile.txt> unixfile.txt: convert Mac-format file to Unix-format file

Miscellaneous

1. su: set user, allow you to become another user, use ‘exit’ to back to normal

2. sudo: set user and do. Execute a single command as another user

sudo –u geoger ls ~geoger/Documents: become geoger and view his Documents

3. ps: list the processes running on the system

ps –ax: list all running processes

ps –aux: list all running processes with additional information about resource usage

4. top: list top CPU consuming processes running

top –us5 display processes sorted by CPU usage, updating 5 s

5. kill: kill process

kill 220: terminate process #220

6. ifconfig: configure network interfaces

ifconfig –a: list all network ports and their settings

7. man: display online documentation

man ls: display commands ls usages

8. apropos: list the manual pages relating to a particular keyword.

Apropos file: list manual pages mention ‘file’ in their summary file

9. clear: clear current screen

10. open: open a file in GUI

open ~Documents/foo: open folder foo

open –a /Applications/TextEdit.app ~/Documents/foo: use TextEdit to open foo

11. find: scan a directory structure for files matching certain criteria

find / -name foo: search entire file structure for files named exactly ‘foo’

find . –name foo: search current directory for foo

find .-mtime -2: search current directory for files modified within 2 days

find . –name *att*: searc h for files names contained ‘att’ in current directory

12. zip/unzip: package and compress files/decompress

13. gzip/ungzip

14. exit

15. reboot

16. shutdown –h now

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s