Coder Social home page Coder Social logo

linuxhelp's Introduction

Linux commands

Help

Command Example Comment
man <command> man cd
man ls
Manual
Get help (close with q).
<command> --help cd --help Also help.
Tab (1x or 2x)   Auto completion.
  See previous command.
Ctrl+C   Kill the current process or command (e.g. if something hangs).
Ctrl+D   Logout. Closes the console if you're not in an ssh session. Similar to exit.
Ctrl+R   Search through your history. Start typing and it will auto-complete. Hit Ctrl+r again and it will cycle though the other auto-completion options. Hit Enter and the command will execute. Hit , to edit commands.

Basics

Command Example Comment
sudo <command> sudo ls Super user do
Run a command with elevated privleges. Will ask you for a password. Only possible, if you were granted administrative rights on the system.
cd <folder> cd test
cd ..
cd -
cd ~
cd /path/to/my/folder
Change directory
. (dot) is the current directory
.. (dotdot) is the upper/partent directory
/ (slash) is the root directory
~ (tilde) is your home directory
- (minus) switches to the previous directory
ls
ls <options>
ls <folder>
ls <pattern>
ls
ls -la
ls -l -a (same as above)
ls -halt (more arguments)
ls -d */ (list all directories)
ls test (contents of subfolder)
ls *.txt (show only .txt files)
List contents of a folder
-h human readable
-a all
-l more information
-t order by time
mkdir <folder>
mkdir -p <path>
mkdir test Make directory
Creates a new immediate subfolder with the given name.
-p Create path.
pwd Print working directory
Shows the current path.
mv  <source> <target> mv text.txt test
mv test.txt bla.txt
Move a file
Can also be used for renaming (second example)
cp <source> <target> cp text.txt test
cp -p text.txt test
Copy a file
-p preserves mode, ownership, and timestamps
Can also rename.
rm <file>
rm -rf <folder>
rm text.txt
rm -rf test
rm *.tmp (removes all files with file ending *.tmp)
Remove
Warning: Cannot be undone!
-f force, no confirmation
dialog, no warnings
-r recursive, for folders
clear Clear the console.
Gives you a fresh view. Similar to Ctrl+L
reset Reset the console.
Like clear but more powerful.

More Basics

Command Example Comment
head <file>
tail <file>
head text.txt
head -n 20 text.txt
Display first/last lines of file
Default n=10.
less <file> less text.txt Display contents of a file 
of a file, read-only
h help
q close
f,b forward, backward one page
e,y forward, backward single line
/<word> search
n,p next, previous <word> during search
-i activate case insentitive search
nano <file> nano text.txt File editor
Ctrl+x to close
Alt+/ to go to the end of a file
chmod chmod 777 test Change permissions
for file
777 gives the folder all possible rights.
Further explanation see below.
chown <username> <file> sudo chown alice folder Change file owner
du <directory> du -h
du -sh .
du -sh * | sort -h
Disk usage
-s summary
-h human readable
df <directory> df -h Disk free
Show remaining disk space.
-h human readable
htop Task manager
View currently running processes. Q to close.

Multiple Commands

Command Example Comment
<command1> ; <command2> Concatenate commands
Execute <command2> after <command1>.
<command1> && <command2> Double ampersand
Execute <command2> after <command1> but only if <command1> finished successfully.
<command> > <file>
<command> >> <file>
<command> 2> <file>
ls -a > result.txt
ls -a >> result.txt
Redirect
the output of a command into a file
> creates/overwrites a file
>> creates/appends to a file
2> redirects the errors
<command1> | <command2> history | less
ls | less
Pipe
the output of a command to less.
Especially useful for history command (displays the latest commands) or folders with many files in them (last example)

Search

fselect - Search for files in a modern, SQL-like fashion.

Command Example Comment
grep    
find    

Screen

Command Comment
screen Create a new session.
Ctrl+A,D Detach from current screen session.
Ctrl+D End current session. Similart to exit.
screen -r Reattach to session.
screen -ls List all sessions.
screen -S <name> -L Create a new screen session <name> with logging enabled.
screen -r <name> Reattach to session with <name> if there are multiple ones.
screen -rx <name> Attach to session that is already attached.
Ctrl+A,Esc Enter scroll mode. Use and or Pg Up and Pg Dn to scroll. Hit Esc to exit scroll mode.

Misc

Command Example Comment
passwd <username> passwd alice Change password
rsync <source> <target> rsync -aP file.txt servername:/home/user/data Rsync
copy files from/to a server
scp <source> <target> scp [email protected]:/my/folder/*.txt . Secure copy 
files from/to a server
-r recursive (include subfolders)
The example copies all files from the given directory then end in .txt to the local directory (dot)
ssh <server>
ssh -t <server> "<command>"
ssh [email protected]
ssh -t [email protected] "ls -a"
Secure shell
Connect to a server
-t Close connection immediately after the command is done
Further explanation see below.
stat <filename> stat text.txt Display file Status, creation date,
last modification date, etc.
su <username> su root Switch user
touch <filename> touch text.txt
touch makefile
Touch a file.
Creates a new, empty file if the file does
not already exist.
Especially helpful to create makefiles under Windows.
Actually the command is used for changing file timestamps.
watch watch -n60 ls Repeat a command every n seconds.
which which nano Display where the command / program is coming from.

Cursor Tricks

Command Comment
Ctrl+A Jump to beginning.
Ctrl+E Jump to end.
Ctrl+W Delete one word left of the cursor.
Ctrl+U Delete entire line.
Ctrl+Y Paste back what you just deleted.

Creating an SSH key

# Creating
ssh-keygen -t rsa -b 4096 -N "" -C "" -f keyname
mv keyname* ~/.ssh

# Setting access rights
chmod 700 ~/.ssh && chmod 600 ~/.ssh/*

# ~/.ssh/config
Host github
HostName github.com
User git
IdentityFile ~/.ssh/keyname

# This logs into the server, and copies the public key to it.
ssh-copy-id -i ~/.ssh/keyname user@remote_machine
# Checking the ssh procesd
ssh -T [email protected]
eval $(ssh-agent -s)
ssh-add ~/.ssh/keyname
ssh -T [email protected]

Permissions

Type chmod xxx <filename> to change permissions where xxx is the numerical code from the table below.

Explaination of the Codes: . ... ... ...
                           (type) (user persmissions) (group permissions) (world permissions)

The first item can be d (a directory), - (a regular file) or l (a symbolic link).
The following three triplets specify permissons for the user, group and world in that order.
In each tripplet, permissions can be r (read), w (write), x (execute) or - (not assigned).
Setting permissions can be done via numbers: r=4, w=2, x=1 and -=0.

Setting Code Use Case
---------- 000 Locking even yourself out. Use chmod again, if this happens.
-r-------- 400 An auto-generated password file (e.g. ~/.google_authenticator).
-rw------- 600 ~/.history, all the ssh keys in your ~/.ssh folder.
-rwx------ 700 Your ~/.ssh folder.
-r--r--r-- 444 A textfile, that others should see as well, but nobody should modify it.
-r-xr-xr-x 555 A folder, that others should be able to cd into as well, but nobody should modify it.
-rwxr-xr-x 755 Files and folders you want other people to see.
-rwxrwxrwx 777 Files and folders you want other people to see and modify. The most open permission.

Permissions on directory have the following meaning:
The read bit allows to list the files within the directory.
The write bit allows to create, rename, or delete files within the directory, and modify the directory's attributes.
The execute bit allows to enter the directory, and access files and directories inside.

To view permissions as numerical code: stat -c %a <filename>.

What does `s` mean? (click to expand) "s", like "x", means something different for directories and regular files.

For files, "x" means "executable" of course. For directories, it means "searchable." Without "x" permission on a directory, you can't set it to be your current directory, or get any of the file information like size, permissions, or inode number, so that you effectively can't access any of the files. If a directory has no "r" permission, you can't get a listing, but if you know a file is there, you can still access the file.

Now "s", for files, means "setuid exec." If a file has s permission, then it's executable, and furthermore, the user id and/or group id of the process is set to the user or group id of the owner of the file, depending on whether it's the user or group "s" that's set. This is a way to give limited root powers to a user -- a program that runs as root when an ordinary user executes it. For example, the "passwd" program, which can change otherwise write-protected files on behalf of a user, works this way: it's owned by the "bin" group (generally) and has g+s so that it can write to /etc/passwd and/or /etc/opasswd which are also owned by group "bin."

For directories, "s" means "sticky". If a directory has "s", then the owner and/or group of any files put into the directory are set to the owner/group of the directory. This is often used on CVS repositories, so that the files in the repository end up all owned by the same person and/or group, even though they're put in by different people. I use g+s on all the CVS repositories I set up.

Snippets and Useful .bashrc Additions

# Finding out which linux you are using
uname -m && cat /etc/*release

# Bulk renaming of files
rename 's/ch0/ch/gi' *.tiff
# Make output of df and du human readable
alias df='df -h'
alias du='du -h'

# Count files in directory
alias fcount='ls -1 | wc -l'

# Disable "Save workspace" promt when closing R
alias R='R --no-save'

Resources

linuxhelp's People

Contributors

r0f1 avatar

Watchers

James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.