SEARCH

Enter your search query in the box above ^, or use the forum search tool.

You are not logged in.

#1 2012-06-09 00:39:46

rhowaldt
#!*$%:)
Registered: 2011-03-09
Posts: 4,396

Command Line: The Absolute Basics

well, i apparently got too much time on my hands. here are some of the most basic, simple, 'omg you didnt know that!?!?' commands (with explanations!) for all you newbies and GUI-addicts wink

lots of times we're taking these for granted, because we use them all the time. but they're apps and commands in their own right. they are the core of the KISS/'do one thing and do it right' philosophy. you'll find these anywhere and they are awesome.

if you're new to this, i highly recommend and encourage you to try these out for yourself. just make yourself a new directory with some more directories and a bunch of files for testing (the commands to do this are somewhere below!)

<stuff between these triangles> means User Input.
# means a comment (so no longer part of the command) (the amount of #'s doesn't matter)

man <command> # get the manual of a command. use this on everything below for even more information!

SOME FILES AND DIRECTORIES STUFF

mkdir <dir> # make a directory
touch <file> # make an (empty) file
ls # list the contents of a directory (from now on: dir)
ls -l # shows you the long version of 'ls'
ls -a # shows you all the files, including hidden files
pwd # tells you which directory you are in
cd <dir> # navigate to <dir>
cd <dir>/<dir>/<dir> # you can guess this one, right?
cd .. # navigate one directory 'up' in the directory tree
mv <file> <dir> # move <file> to <dir>
mv -i <file> <dir> # move <file> to <dir> with confirmation on overwrite (-i = --interactive)
mv <file> <new filename> # change the filename of <file> to <new filename>
cp <file> <new file> # copy <file> to <new file>
cp -i <file> <new file> # copy <file> to <new file> with confirmation on overwrite
cp <file> <dir> # copy <file> to <dir> (keeping its name)
cp -r <dir> <other dir> # you cannot copy directories without using the -r (recursive) option
rm <file> # remove <file>
rm -i <file> # remove <file> with confirmation
rm -r <dir> # you cannot remove a directory without using the -r option

SOME FILE DISPLAY AND INFO AND REDIRECTION

clear # clears the screen
echo "a big pile of poop" # prints "a big pile of poop" to your screen. try it!
cat <file> # concatenate (errr... display?) the contents of a file. gives you sensible output on text-files etc, and messes up your terminal on compiled stuff.
file <file> # tells you what kind of file it is

this symbol: | is a pipe. you use it to pipe the output of one command to the input of another. for example:

cat sometext.txt | more # pipes the output of the cat-command to the more-command (more on that later! :D)

this symbol: > redirects the output of a command to a file. for example:

ls > thisisinmydirectory.txt # you now have a file which has the contents of your directory! jeeej!

this symbol: >> is the same as > but appends it to the end of the file, so it doesn't overwrite what is already there. example:

[02:04:50]$ echo "meh" > bla.txt
[02:04:56]$ cat bla.txt
meh
[02:04:58]$ echo "huu" >> bla.txt
[02:05:09]$ cat bla.txt
meh
huu

SOME MORE STUFF

more alongtextfile.txt

if your text-file is long, 'more' stops it from scrolling off the screen. press [SPACE] to see the next screen. press 'q' to quit.

less alongtextfile.txt

same as more. what? it's called less, but does more than more. confused? read here for more (hahaha): http://crunchbanglinux.org/forums/topic … x-is-more/
both more and less are called 'pagers'. if you tried the 'man' command, you've already seen one of them. to see what kind of pager you use by default, just check out the $PAGER global variable, like so:

[02:05:49]$ echo $PAGER
less -X

i use less -X. now you can figure out what that means smile

ADVANCED STUFF (or not so advanced but okay)
the symbol ~ means your 'home'. in my case, /home/rhowaldt. this is the same as $HOME:

[02:22:58]$ echo ~
/home/rhowaldt
[02:22:59]$ echo $HOME
/home/rhowaldt

the symbol: . means the current directory. so this:

cp /media/storage/mp3/* .

means: copy everything inside of the /media/storage/mp3 directory to my current directory.

mv *.mp3 /media/storage/mp3

moves all the files ending in .mp3 inside the current directory to the directory /media/storage/mp3. the * means 'everything'. it is a Regular Expression. i'm not going to explain any more than *, but here's a cheatsheet: http://krijnhoetmer.nl/stuff/regex/cheat-sheet/

su # switch user. without arguments, switches to the root-user
sudo <command> # makes you root temporarily and executes the command. example:
sudo apt-get install gimp

SEARCH

which <command> # tells you which command will be executed when you type 'command'. example:
[02:24:20]$ which python
/usr/bin/python

apropos <something> # searches the manualpages for <something>
find . -name "poop" # starts searching from the current directory (.) and looks for filenames with 'poop'
find / -name "mehh" # starts searching from the root (/) directory and looks for filenames with 'mehh'
cat mytextfile.txt | grep "chewbacca"

returns any lines that have 'chewbacca' in them. grep is too huge to explain here. please just Google about this if you want more info. the same goes for sed and awk, which i'm not even going to start about.

exit # take a wild guess!

so, that is about it as far as the absolute basics go. if you are getting enthusiastic, here are some more links for further reading:
http://crunchbanglinux.org/forums/topic … and-lines/ - whole bunch of other commandlines
http://crunchbanglinux.org/forums/topic … resources/ - the terminal by default uses a language called 'bash'. here is everything you basically need to know about that gathered in one place.

have some fun! big_smile

Offline

Help fund CrunchBang, donate to the project!

#2 2012-06-09 00:49:38

ivanovnegro
Ivan #000000
From: unstable madness
Registered: 2011-06-02
Posts: 5,426

Re: Command Line: The Absolute Basics

Omg! big_smile

Offline

#3 2012-06-09 01:05:36

pidsley
Window Mangler
Registered: 2012-05-23
Posts: 1,752

Re: Command Line: The Absolute Basics

sweet... (and OMG!)

Offline

#4 2012-06-09 01:12:36

arclance
#! Die Hard
Registered: 2012-03-29
Posts: 902

Re: Command Line: The Absolute Basics

Very nice.
Do you think that some simple ssh/scp commands would fit?

ssh is useful if you need to kill something that is locking up X.
scp lets you copy files over a network a lot easier than setting up nfs or samba.

Offline

#5 2012-06-09 08:21:30

Tunafish
#! Die Hard
From: the Netherlands
Registered: 2010-03-07
Posts: 1,204

Re: Command Line: The Absolute Basics

Rhowaldt wrote:

well, i apparently got too much time on my hands.

Exactly what I thought when I saw the title. smile

Edit: -- never mind --

Last edited by Tunafish (2012-06-09 08:34:07)


sed 's/stress/relaxation/g'
Privacy & Security on #!

Offline

#6 2012-06-09 10:16:27

sano
#! CrunchBanger
Registered: 2012-03-07
Posts: 114

Re: Command Line: The Absolute Basics

You are trying to create a replacement for the Arch wiki here, right?

Offline

#7 2012-06-09 11:09:38

Unia
#! Octo-portal-pussy
From: The Netherlands
Registered: 2010-07-17
Posts: 3,956

Re: Command Line: The Absolute Basics

sano wrote:

You are trying to create a replacement for the Arch wiki here, right?

We can always fork it lol


If you can't sit by a cozy fire with your code in hand enjoying its simplicity and clarity, it needs more work. --Carlos Torres
Github

I am a #! forum moderator. Feel free to send me a PM with any question you have!

Offline

#8 2012-06-09 11:19:33

rhowaldt
#!*$%:)
Registered: 2011-03-09
Posts: 4,396

Re: Command Line: The Absolute Basics

thanks for all the nice/funny/weird comments you guys big_smile

Offline

#9 2012-06-10 00:53:02

sunfizz98
Carbonated Orange Juice
From: su terminal
Registered: 2011-05-12
Posts: 1,876
Website

Re: Command Line: The Absolute Basics

An awesome thread already big_smile

Offline

#10 2012-06-11 20:54:40

Ed Flanders
#! Member
From: cl
Registered: 2011-10-26
Posts: 59

Re: Command Line: The Absolute Basics

Thank you very much.
Having learned to use the "terminal" in Windows, I was always using cd.. to move upward in directory trees... little did I know that I needed a space tongue
I think I'll save a local copy of this post just in case.


I've made a huge mistake

Offline

#11 2012-06-11 21:17:22

griffen76
Member
Registered: 2012-06-01
Posts: 14

Re: Command Line: The Absolute Basics

cd    #will take you to the Home directory of the user you are at that time
cat > filename  # will input whatever you type into a filr until you hit ctrl-d

Last edited by griffen76 (2012-06-11 21:17:49)

Offline

#12 2012-06-11 21:27:56

Awebb
The Singularity
Registered: 2009-07-23
Posts: 2,812

Re: Command Line: The Absolute Basics

moderator's note: classic fork bomb; do not use unless raising elephants is so utterly boring.

cd -                # will return you to the last directory you were before the current one
:(){ :|:&};:        # allows you to play tetris in your tty

Last edited by pvsage (2013-12-11 19:10:20)


I'm so meta, even this acronym

Offline

#13 2012-06-11 21:36:26

Eren
#! Die Hard
From: Black Mesa
Registered: 2009-07-27
Posts: 830

Re: Command Line: The Absolute Basics

I never played a hot tetris. İ did now. Thanks.

Offline

#14 2012-06-12 01:16:51

gutterslob
#! Resident Bum
Registered: 2009-11-03
Posts: 3,078

Re: Command Line: The Absolute Basics

I cannot believe you started a thread on command line basics without including the most basic of commands!!

w

Your life in one, single-letter command lol

Last edited by gutterslob (2012-06-12 01:17:07)


Point & Squirt

Offline

#15 2012-06-12 08:17:11

rhowaldt
#!*$%:)
Registered: 2011-03-09
Posts: 4,396

Re: Command Line: The Absolute Basics

@Awebb: you're deploying the fork-bomb again i see smile

@gutterslob: well, i have a single-user system so i never really use 'w', which is why i forgot!

Offline

#16 2012-06-12 08:42:49

aiBo
#! CrunchBanger
Registered: 2010-11-08
Posts: 241

Re: Command Line: The Absolute Basics

Nice overview for the basics, rhowaldt.

But I have one question. I guess for every cat used in this combination a kittie is tortured

rhowaldt wrote:
cat mytextfile.txt | grep "chewbacca"

Don't you like kitties? wink

Why don't you use

grep "chewbacca" mytextfile.txt

Offline

#17 2012-06-12 10:32:44

rhowaldt
#!*$%:)
Registered: 2011-03-09
Posts: 4,396

Re: Command Line: The Absolute Basics

^ yes, you are right, of course. however, i think that is 'advanced' stuff. maybe i should've used another example, but i took the one i thought of the first, even though it is a waste of resources smile

Offline

#18 2012-06-12 10:53:16

machinebacon
#! unstable
From: China
Registered: 2009-07-02
Posts: 6,631
Website

Re: Command Line: The Absolute Basics

Offline

#19 2012-06-12 11:11:01

Awebb
The Singularity
Registered: 2009-07-23
Posts: 2,812

Re: Command Line: The Absolute Basics

I've been telling these people up and down to avoid pipes, but they obviously don't care, like the honeybadger. I'm not sure, this is quite a basic thing.

Avoid pipes. Use the tool's own filter options and input calls.

WRONG

cat file.txt | grep "burp"
find /home | grep mynudes*jpg

RIGHT

grep "burp" file.txt
find /home -iname mynudes*jpg

BUT

find /home | egrep (a$$l0ngr3gu14r3xpr3550n|really\ long\ regular\ expression)
locate beer | grep "locate has no real filter so we use grep.txt"

Remember, piping usually works well, as in 'it does the job', but you will find many things not working as they should, as in piping or redirecting text via sudo. There are also many tools not accepting standard output from the pipe. Avoiding pipes is considered to be good style, because of such pitfalls. Another reason is that every time you use a pipe, a new process is forked, using CPU and RAM. This might sound irrelevant, if you only invoke single line commands once in a while, but the faster you learn good style, the less trouble will you have later unlearning those old habits.


Writing to a file not belonging to the user with sudo:

echo "text" | sudo tee /etc/text.conf

This will overwrite the file text.conf with the single word "text"

echo "text" | sudo tee -a /etc/text.conf

This will append "text" in a new line of the file text.conf


Producing a command line from many lines of standard output:

locate beer | xargs rm

Locate produces output like this:

/usr/share/zim/manual/Help/Auto_Formatting.txt
/usr/share/zim/manual/Help/Check_Boxes.txt
/usr/share/zim/manual/Help/Config_Files.txt
/usr/share/zim/manual/Help/Custom_Tools.txt
/usr/share/zim/manual/Help/Export.txt
/usr/share/zim/manual/Help/Importing_external_files.txt
/usr/share/zim/manual/Help/Key_Bindings.txt
/usr/share/zim/manual/Help/Links.txt
/usr/share/zim/manual/Help/Menu_Items.txt
/usr/share/zim/manual/Help/Notebooks.txt
/usr/share/zim/manual/Help/Pages.txt
/usr/share/zim/manual/Help/Preferences.txt
/usr/share/zim/manual/Help/Properties.txt
/usr/share/zim/manual/Help/Searching.txt
/usr/share/zim/manual/Help/Tags.txt
/usr/share/zim/manual/Help/Templates.txt
/usr/share/zim/manual/Help/Wiki_Syntax.txt
/usr/share/zim/manual/Help/Export/HTML.txt
/usr/share/zim/manual/Help/Export/LaTeX.txt
/usr/share/zim/manual/Plugins/Arithmetic.txt
/usr/share/zim/manual/Plugins/Automount.txt
/usr/share/zim/manual/Plugins/Calendar.txt
/usr/share/zim/manual/Plugins/Diagram_Editor.txt
/usr/share/zim/manual/Plugins/Ditaa_Editor.txt
/usr/share/zim/manual/Plugins/Equation_Editor.txt
/usr/share/zim/manual/Plugins/GNU_R_Plot_Editor.txt
/usr/share/zim/manual/Plugins/Gnuplot_Editor.txt
/usr/share/zim/manual/Plugins/Inline_Calculator.txt
/usr/share/zim/manual/Plugins/Insert_Screenshot.txt
/usr/share/zim/manual/Plugins/Insert_Symbol.txt
/usr/share/zim/manual/Plugins/Link_Map.txt
/usr/share/zim/manual/Plugins/Quick_Note.txt
/usr/share/zim/manual/Plugins/Spell_Checker.txt
/usr/share/zim/manual/Plugins/Table_Of_Contents.txt
/usr/share/zim/manual/Plugins/Tags.txt
/usr/share/zim/manual/Plugins/Task_List.txt
/usr/share/zim/manual/Plugins/Tray_Icon.txt
/usr/share/zim/manual/Plugins/Version_Control.txt
/usr/share/zim/manual/Usage/Daily_Journal.txt
/usr/share/zim/manual/Usage/Getting_Started.txt
/usr/share/zim/manual/Usage/Getting_Things_Done.txt
/usr/share/zim/templates/_quicknote.txt
/usr/share/zim/templates/wiki/Calendar.txt
/usr/share/zim/templates/wiki/Default.txt
/var/lib/pacman/local/xls2txt-0.12-1
/var/lib/pacman/local/xls2txt-0.12-1/desc
/var/lib/pacman/local/xls2txt-0.12-1/files

Piping it through xargs will transform the output to a single line:

/usr/share/vim/vim73/doc/windows.txt /usr/share/vim/vim73/doc/workshop.txt /usr/share/vim/vim73/ftplugin/README.txt /usr/share/vim/vim73/indent/README.txt /usr/share/vim/vim73/keymap/README.txt /usr/share/vim/vim73/lang/README.txt /usr/share/vim/vim73/macros/README.txt /usr/share/vim/vim73/macros/matchit.txt /usr/share/vim/vim73/macros/maze/README.txt /usr/share/vim/vim73/macros/urm/README.txt /usr/share/vim/vim73/plugin/README.txt /usr/share/vim/vim73/syntax/README.txt /usr/share/vim/vim73/tools/README.txt /usr/share/vim/vim73/tools/ccfilter_README.txt /usr/share/vim/vim73/tools/efm_filter.txt /usr/share/vim/vim73/tools/mve.txt /usr/share/vim/vim73/tools/vimspell.txt /usr/share/vim/vim73/tutor/README.el.cp737.txt /usr/share/vim/vim73/tutor/README.el.txt /usr/share/vim/vim73/tutor/README.txt /usr/share/vim/vimfiles/doc/LargeFile.txt /usr/share/wmclock/lang.polish/month.txt /usr/share/wmclock/lang.polish/weekday.txt /usr/share/zim/manual/About.txt /usr/share/zim/manual/Bugs.txt /usr/share/zim/manual/FAQ.txt /usr/share/zim/manual/Help.txt /usr/share/zim/manual/Plugins.txt /usr/share/zim/manual/Start.txt /usr/share/zim/manual/Usage.txt /usr/share/zim/manual/Help/Auto_Formatting.txt /usr/share/zim/manual/Help/Check_Boxes.txt /usr/share/zim/manual/Help/Config_Files.txt /usr/share/zim/manual/Help/Custom_Tools.txt /usr/share/zim/manual/Help/Export.txt /usr/share/zim/manual/Help/Importing_external_files.txt /usr/share/zim/manual/Help/Key_Bindings.txt /usr/share/zim/manual/Help/Links.txt /usr/share/zim/manual/Help/Menu_Items.txt /usr/share/zim/manual/Help/Notebooks.txt /usr/share/zim/manual/Help/Pages.txt /usr/share/zim/manual/Help/Preferences.txt /usr/share/zim/manual/Help/Properties.txt /usr/share/zim/manual/Help/Searching.txt /usr/share/zim/manual/Help/Tags.txt /usr/share/zim/manual/Help/Templates.txt /usr/share/zim/manual/Help/Wiki_Syntax.txt /usr/share/zim/manual/Help/Export/HTML.txt /usr/share/zim/manual/Help/Export/LaTeX.txt /usr/share/zim/manual/Plugins/Arithmetic.txt /usr/share/zim/manual/Plugins/Automount.txt /usr/share/zim/manual/Plugins/Calendar.txt /usr/share/zim/manual/Plugins/Diagram_Editor.txt /usr/share/zim/manual/Plugins/Ditaa_Editor.txt /usr/share/zim/manual/Plugins/Equation_Editor.txt /usr/share/zim/manual/Plugins/GNU_R_Plot_Editor.txt /usr/share/zim/manual/Plugins/Gnuplot_Editor.txt /usr/share/zim/manual/Plugins/Inline_Calculator.txt /usr/share/zim/manual/Plugins/Insert_Screenshot.txt /usr/share/zim/manual/Plugins/Insert_Symbol.txt /usr/share/zim/manual/Plugins/Link_Map.txt /usr/share/zim/manual/Plugins/Quick_Note.txt /usr/share/zim/manual/Plugins/Spell_Checker.txt /usr/share/zim/manual/Plugins/Table_Of_Contents.txt /usr/share/zim/manual/Plugins/Tags.txt /usr/share/zim/manual/Plugins/Task_List.txt /usr/share/zim/manual/Plugins/Tray_Icon.txt /usr/share/zim/manual/Plugins/Version_Control.txt /usr/share/zim/manual/Usage/Daily_Journal.txt /usr/share/zim/manual/Usage/Getting_Started.txt /usr/share/zim/manual/Usage/Getting_Things_Done.txt /usr/share/zim/templates/_quicknote.txt /usr/share/zim/templates/wiki/Calendar.txt /usr/share/zim/templates/wiki/Default.txt /var/lib/pacman/local/xls2txt-0.12-1 /var/lib/pacman/local/xls2txt-0.12-1/desc /var/lib/pacman/local/xls2txt-0.12-1/files

You can now feed it to whatever is taking single lines of arguments. Like a list of urls for firefox.

cat linklist.txt | xargs firefox

We need cat here, as firefox does not accept text files for link input, it would open the file instead.

Last edited by Awebb (2012-06-12 11:12:30)


I'm so meta, even this acronym

Offline

#20 2012-06-12 14:09:38

rhowaldt
#!*$%:)
Registered: 2011-03-09
Posts: 4,396

Re: Command Line: The Absolute Basics

^ thanks for the elaborate explanation Awebb. that's one i couldn't have written so clearly myself.

Offline

#21 2012-06-13 17:44:10

johnraff
#!Drunkard
From: Nagoya, Japan
Registered: 2009-01-07
Posts: 2,927
Website

Re: Command Line: The Absolute Basics

Awebb wrote:

Producing a command line from many lines of standard output:

locate beer | xargs rm

What about filenames with spaces in? You might be deleting what you didn't expect. With find you can use the -print0 option, and the -0 option with xargs to avoid this problem. Locate's -0 option might work here. ie

locate -0 beer | xargs -0 rm

But I haven't tried it.


John
--------------------
( a boring Japan blog , idle twitterings  and GitStuff )
#! forum moderator

Offline

#22 2012-06-13 22:29:38

wuxmedia
wookiee madclaw
From: Back in Blighty
Registered: 2012-03-09
Posts: 1,472
Website

Re: Command Line: The Absolute Basics

i like ctrl - r (search cmd line history) hit that, start typing 'sm' and (if it's been typed already) boom -  'sudo geany /etc/samba/smb.conf' appears.
useful if, like me, you need to run the same cmds over and over..

ohh and- Shift- Pg-up  -should- scroll up,(and pg- dwn - well, down) sometimes real handy in a 'real' console. ie S*l*n* G*m*z!!  what was the output from the ls -a i did at the beginning - or oops i forgot to pipe that huge dir listing to more (or less).... tongue

I 'member even on DOS one could use a 'switch' to list 'dir' one page at a time...

Last edited by wuxmedia (2012-06-13 22:31:58)

Offline

#23 2012-06-13 23:02:59

Awebb
The Singularity
Registered: 2009-07-23
Posts: 2,812

Re: Command Line: The Absolute Basics

johnraff wrote:

With find you can use the -print0 option, and the -0 option with xargs to avoid this problem.

Oh, that's useful.

Locate doesn't seem to be the scripter's friend anyway. However, this is nothing another pipe and sed can't fix :-D


I'm so meta, even this acronym

Offline

#24 2012-07-20 16:49:34

rhowaldt
#!*$%:)
Registered: 2011-03-09
Posts: 4,396

Re: Command Line: The Absolute Basics

just dropping in to link to another post that might be useful to newcomers for learning a bit about this stuff: http://crunchbanglinux.org/forums/post/244133/#p244133

Offline

Be excellent to each other!

#25 2012-07-20 17:09:11

mariannemarlow
#! Die Hard
From: My flat, London, England
Registered: 2012-06-03
Posts: 2,209
Website

Re: Command Line: The Absolute Basics

<> these are not triangles rhowaldt they are less than and greater than symbols

tongue

But thank you for the guide big_smile

Last edited by mariannemarlow (2012-07-20 17:09:31)


Between two evils, I always pick the one I never tried before ~ Mae West

Offline

Board footer

Powered by FluxBB

Copyright © 2012 CrunchBang Linux.
Proudly powered by Debian. Hosted by Linode.
Debian is a registered trademark of Software in the Public Interest, Inc.

Debian Logo