Difference between revisions of "Basic Linux Use"

From WebOS Internals
Jump to navigation Jump to search
 
Line 56: Line 56:
 
Many functions cannot be carried out unless the logged-in username has proper permissions.  sudo will run a command or series of commands as root, although it may require a password.
 
Many functions cannot be carried out unless the logged-in username has proper permissions.  sudo will run a command or series of commands as root, although it may require a password.
  
''Ex: "sudo mount -o remount,rw /"'' would mount the file system as read-write (needed for many changes, and needs root permissions)
+
''Ex: "sudo mount -o remount,rw /"'' would mount the file system as read-write (needed for many changes, and needs root permissions).
 +
 
 +
''Ex: "sudo vi <locked filename>"'' would edit a file whose permissions are locked-down for the current user.
  
 
== Editing Files using vi ==
 
== Editing Files using vi ==

Latest revision as of 00:38, 29 July 2009

Tux-pre.jpg

Linux at its core runs a command line shell similar to that of DOS and the Windows Command Prompt, and like the Windows/DOS Command Line, you can seriously harm your OS if you just go poking around without any idea as to what it is you're doing. The commands and programs below are the most used commands and programs throughout the tutorials on this site. Please bear in mind that Linux commands and file names are case sensitive.

Command Line Commands

cd

Dos Eqiv: cd Used to change directories. To go up a level use '..'

Ex: "cd hello" would move one directory down to /hello/

Ex: "cd .." would take you to the directory that is parent to your Present Working Directory. If you are in /usr/lib, this command will take you to /usr

pwd

Dos Eqiv: cd (with no parameters) Used to show the Present Working Directory

Ex: "pwd" would display your current location in the filesystem tree

ls

Dos Eqiv: dir Used to list the contents of a directory.

Ex: "ls" would list the contents of where you currently are. Ex: "ls /hello/" would list the contents of /hello/ no matter where you are.

cp

Dos Eqiv: copy Used to copy files from one spot to another. Adding the -i flag will cause cp to ask if you want to overwrite the destination file if it already exists. Use of this flag is recommended to prevent accidental overwriting of previous backups.

Ex: "cp -i test test.bak" would copy test to test.bak

Ex: "cp -i test /var/test.bak" would copy test to /var/test.bak

mv

Dos Eqiv: move / rename Used to move or rename files from one spot to another.

Ex: "mv oldname.file newname.file" will rename the file oldname.file to newname.file

Ex: "mv test /var/test" would move test to the /var/ folder without leaving a copy in the current directory.

rm

Dos Eqiv: del Used to delete files and directories. Must use the -r option to delete directories.

Ex: "rm test.file" would delete the test.file

Ex: "rm test*" would delete all files whose name starts with 'test'

Ex: "rm /test -r" would delete the folder named 'test'

Note: Adding -rf or -Rf to the rm command will "force" deletion of files, folders, and folder contents without confirmation. Please be very mindful of any instructions that suggest doing this, and don't rm -Rf anything unless you know what you're doing.

sudo

Many functions cannot be carried out unless the logged-in username has proper permissions. sudo will run a command or series of commands as root, although it may require a password.

Ex: "sudo mount -o remount,rw /" would mount the file system as read-write (needed for many changes, and needs root permissions).

Ex: "sudo vi <locked filename>" would edit a file whose permissions are locked-down for the current user.

Editing Files using vi

The Pre's Linux operating system comes installed with a text editing program called vi. It's a very simple to use text editor.

To start with your start vi using the command 'vi' followed by the file you would like to edit.

Ex: "vi test.html"

Ex: "vi /var/test.html"

Once you start vi you will notice that you can't just start typing and have it show up in the file. vi has two modes, an Editing and a Command mode. vi always starts in the command mode. The keys you can use to switch between the two modes are show below:

Switch to Editing Mode

i	Insert text before the cursor
I	Insert text at the beginning of the current line
a	Append text after the cursor
A	Append text to the end of the current line
o	Append text to new line after current line
O	Append text to new line above current line
cw	Delete from the cursor to the end of the current word and begin inserting text
c$	Delete from the cursor to the end of the current line and begin appending text

Switch back to Command Mode

esc

Command mode is where you can chose to save a file or close it without saving along with some other various commands that are listed below. You almost always start a command in vi by using the ':' symbol, though sometimes a single character will also perform an action while in command mode.

Saving and Quitting

:q	Quit (only use if no changes have been made since the last save)
:q!	Quit without saving changes
:w	Save the file without quitting vi
:wq	Save the file and then quits vi

Moving Around Within the File

:1	Move to the first line.  Replace 1 with any line number to jump to that line.
G	Move to the last line.
0	Move to the beginning of the current line.
$	Move to the end of the current line.

Special Edit Commands (use while in command mode)

yy	Yank the current line into the buffer (similar to Windows' copy) or yank X number of lines ('4yy' will yank 4 lines)
p	Paste the contents of the buffer below the current line (similar to Windows' paste)
P	Paste the contents of the buffer above the current line (similar to Windows' paste)
dd	Delete the current line, or delete X number of lines ('4dd' will delete 4 lines)
d$	Delete everything from the cursor to the end of the current line
dw	Delete everything from the cursor to the end of the current word
x	Delete the character under the cursor

Search for Text Within the File

/term	Search forward for term
?term	Search backward for term
n	Go to the next instance, if any
N	Go to the previous instance, if any

Examples:

/hello world Search forward in the file, looking for the phrase 'hello world'

?hello world Search backward in the file, looking for the phrase 'hello world'

Note: For a less powerful but easier to use text editor, install nano from the optware repository