Friday, 29 March 2013

Unix Cheat Sheet


List a directoryAlternatively, type 'ls --help' or 'man ls' for more information
ls [path]list given path without any options. Without the path, it is listing current directory
ls -l [path]list given path in long listing format
ls -h [path]list given path in human readable format
ls -a [path]list given path, including hidden files (which has prefix dot)
ls -lrt [path]list given path, combining different options. long listing format, sort by modification time in reverse order
ls [path] list given path, redirect listing results into a file
ls [path] | morelist given path, showing listing results on one screen at a time

Change to a directory
cd <directory>change to the given directory
cd ~change to user's home directory
cd /change to root directory
cd ..go to parent directory
cd ../<directory>go to a sibling directory

Print current working directory
pwddisplay the current working directory

Create directory(ies)
mkdir <directory>create the directory under current working directory
mkdir -p <dir1>/<dir2>create directory2 under directory1; create parent directory2 as needed

Remove files or directory(ies)
rm <file>remove the file
rm -rf <directory>remove the contents of given directory recursively without prompt

Move a file or a directory
mv <source> <dest>rename source as dest, and move source to dest

Copy a file or a directory
cp <source> <dest>make a copy from source to dest
cp -r <source> <dest>copy recursively from source directory to dest directory

Download a file from http website
wget <url>download the resource to current directory
curl <url>download the resource to current directory

View a text file
less <file>view file page by page, with a lot of features
more <file>view file with screen length
cat <file>view file, but it will scroll to the end of file
cat <file> | moreview file, and piped the file content to more command

Create a text file
touch <file>create an empty file
vi <file>use vi text editor to create a file (need to save the file though)
echo "foo" >> <file>create a file with content 'foo'
cat > <file>enter multi-line texts and press control-d to save the content to a file

Change a file's modification or access time
stat <file1>display the stat of given file
touch -m -d '1 Mar 2013 02:53' file.txtchange the modification time of file.txt to be '1 Mar 2013 02:53', and create the file if it doesn't exist
touch -a -d '1 Mar 2013 02:53' file.txtchange the access time of file.txt to be '1 Mar 2013 02:53', and create the file if it doesn't exist

Compare two files
diff <file1> <file2>show the differences between file1 and file2
sdiff <file1> <file2>show file1 and file2 side by side

Pipes and redirections
<command> > <file>redirect command output to a file, e.g. ll > file.txt writes current directory listings to file.txt
<command> >> <file>appends command output to the end of a file
<command> < <file>redirect the standard input from the file
<command> < <file1> > <file2>redirect the standard input from the file1, and then redirect the standard output to file2
<command1> | <command2>pipe the output of command1 as the input of command2

File permissionsread=4, write=2, execute=1
chmod 700 <file(s)>file(s) owner can read, write and execute
chmod 600 <file(s)>file(s) owner can read and write, but not execute
chmod 400 <file(s)>file(s) owner can read only
chmod 755 <file(s)>file(s) owner can read, write and execute; both group owner and others can read and write
chmod 644 <file(s)>file(s) owner can read and write; both group owner and others can read only
chmod u+x <file(s)>grant file(s) owner execute permission
chmod u-x <file(s)>remove execute permission from file(s) owner
chmod a+rw <file(s)>grant everyone read and write permission to the file(s)

Other useful comands
df -hreport file system disk space usage in human readable format
du -hestimate file space usage in human readable format
datedisplay or set the current system date and time
topdisplay linux tasks, as well as CPU and memory stats
ps -aefdisplay a snapshot of current processes
ln -s <target> <link_name>create a symbolic link to the target with link_name
aliasdisplay current defined aliases or create an alias
!<command>invoke previous run of the given command

No comments:

Post a Comment