zeldor.biz

Linux, programming and more

Copyright © 2025
Log in

What does this “> /dev/null 2>&1″ mean?

July 18, 2010 by Igor Drobot Leave a Comment

I remember being very confused for a very long time about the trailing garbage in commands that I saw in Linux systems.

Now I can explain it to you with a very easy example.

Standard in, out, and error

There are 3 standard sources of input and output for a program. Standard input usually comes from the keyboard if it’s an interactive program, or from another program if it’s processing the other program’s output. The program usually prints to standard output, and sometimes prints to standard error. These three file descriptors (you can think of them as “data pipes”) are often called STDIN, STDOUT and STDERR

Sometimes they’re not named, they’re numbered! The built-in numberings for them are 0, 1, and 2, in that order. By default, if you don’t name or number one explicitly, you’re talking about STDOUT.

Given that context, you can see the command above is redirecting standard output into /dev/null, which is a place you can dump anything you don’t want (often called the bit-bucket), then redirecting standard error into standard output (you have to put an & in front of the destination when you do this).

Will create a new directory

1
mkdir test

mkdir test

if you execute it second time you will get a error.
With this redirection you will suppress the error.

1
mkdir test > /dev/null 2>&1

mkdir test > /dev/null 2>&1

The short explanation, therefore, is “all output from this command should be shoved into a black hole.” That’s one good way to make a program be really quiet! That means, if you have some critical errors in your code, you will never see them.

Filed Under: Bash, Linux Tagged With: bash, dev null, Linux, output redirection

Chmod quite different

June 23, 2010 by Igor Drobot Leave a Comment

1
chmod -R u=rwx,g=rwx,o=rx white/

chmod -R u=rwx,g=rwx,o=rx white/

u = User
g = Group
o = Other

Filed Under: Bash, Linux Tagged With: Chmod, Linux, Unix

Sed replace text

June 21, 2010 by Igor Drobot Leave a Comment

Sed is available on all GNU/Linux distributions there is also a versions for Mac OS X too.
Typical usage of Sed are simple text replacement, testing for substrings, complex text replacement with regular expressions.

1
2
cat example.conf 
foo+bar=foo

cat example.conf foo+bar=foo

1
sed -i 's/foo/foo_bar/g' example.conf

sed -i 's/foo/foo_bar/g' example.conf

foo in file example.conf will be replaced with foo_bar

After replacement:

1
foo_bar+bar=foo_bar

foo_bar+bar=foo_bar

Start a dry run without changes, you will get a output about the changes (your file will stay untouched):

1
sed 's/192.168.0.1/nameserver.local/g' example.conf

sed 's/192.168.0.1/nameserver.local/g' example.conf

Append after match:

1
sed '/PATTERN_TO_SEARCH/a APPEND_ME' my_file

sed '/PATTERN_TO_SEARCH/a APPEND_ME' my_file

Remove line number 134 from file:

1
sed -i 134d /root/.ssh/known_hosts

sed -i 134d /root/.ssh/known_hosts

MAC OS X Section!

Sed inplace editing (-i) is a kind different on Mac OS X: in the normal UNIX case you will get a error:

1
2
id$ sed -i '/anna/ d' testfile
sed: 1: "testfile": undefined label 'estfile'

id$ sed -i '/anna/ d' testfile sed: 1: "testfile": undefined label 'estfile'

Soo this is the right syntax:

1
sed -i '' '/anna/ d' testfile

sed -i '' '/anna/ d' testfile

By the way, this will delete all lines which contains “anna” ;)

From the manpage: you risk corruption or partial content in situ-ations where disk space is exhausted, etc.

Filed Under: Linux Tagged With: Linux, regex, replace, sed

GIT Version Control System

June 16, 2010 by Igor Drobot Leave a Comment

GIT is one of the best systems for version control. I don’t want talk about benefits or other things now. I tell you how to work with GIT and do really useful things with it. Very simple installation and a list of frequently used commands:







Install GIT: (You see It’s very easy)

1
apt-get update && apt-get install git-core

apt-get update && apt-get install git-core

Create new git repository:

2
git init

git init

Add all changes to repository:

3
git add .

git add .

Consolidation of all changes:

4
git commit -a

git commit -a

Consolidation of all changes with commit message:

5
git commit -a -m "Commit Message"

git commit -a -m "Commit Message"

Revert last commit:

6
git reset --soft HEAD^

git reset --soft HEAD^

Current working directory status:

7
git status

git status

Diff since last commit:

8
git diff

git diff

Add link to remote repository:

9
git remote add origin ssh://id@gitcher/opt/repository

git remote add origin ssh://id@gitcher/opt/repository

Remove link to remote repository:

10
git remote rm origin

git remote rm origin

Get all new versions from source(remote repository):

11
git pull origin master

git pull origin master

Push all new changes to source(remote repository):

12
git push origin master

git push origin master

Create new working tree(branch):

13
git branch branch_name

git branch branch_name

Change between branches:

14
git checkout branch_name

git checkout branch_name

Merge braches:

15
git merge branch_name

git merge branch_name

Delete branch:

16
git branch -d branch_name

git branch -d branch_name

Remove branch force:

17
git branch -D branch_name

git branch -D branch_name





And more complicated examples
Create local working space and push all changes to remote repository:

1
2
3
4
5
6
git init
touch Initial_File
git add Initial_File
git commit -a -m "Initial commit"
git remote add origin ssh://id@gitcher/opt/repository
git push origin master

git init touch Initial_File git add Initial_File git commit -a -m "Initial commit" git remote add origin ssh://id@gitcher/opt/repository git push origin master



Create brach, edit and merge:

1
2
3
4
5
6
7
8
9
10
git init
git branch test
git checkout test
# ...
# add changes
# ...
git commit -a -m "Branch commit"
git checkout
git merge test
git branch -d test

git init git branch test git checkout test # ... # add changes # ... git commit -a -m "Branch commit" git checkout git merge test git branch -d test

Messed up the last commit message? This will let you re-enter it:

1
git commit --amend

git commit --amend

GIT Tagging
for example you want to release a new version of your project:

1
git tag -a v0.1 -m 'Intitial tag vesrion 0.1'

git tag -a v0.1 -m 'Intitial tag vesrion 0.1'

Clone remote repositoty

1
git clone id@gitche:scripts.git

git clone id@gitche:scripts.git

Filed Under: Linux Tagged With: Control, create branch, create repository, Debian, GIT, git commit, Linux

What run level am I in?

June 13, 2010 by Igor Drobot Leave a Comment

Q: How can I find out what run level a system is in at the moment?

1. The runlevel command tells you what run level is running
2. The who -r command also tells you the run level

1
2
firewall ~ # /sbin/runlevel
N 2

firewall ~ # /sbin/runlevel N 2

or

1
2
firewall ~ # who -r
         run-level 2  2010-05-28 23:13

firewall ~ # who -r run-level 2 2010-05-28 23:13

Filed Under: Linux Tagged With: Linux, runlevel

  • « Previous Page
  • 1
  • 2
  • 3
  • 4
  • Next Page »
Yeaaah Cookie! We use cookies to ensure that we give you the best experience on our website. If you continue to use this site we will assume that you are happy with it.Ok