All large systems, which today means any system, have thousands of files. On this laptop that I'm writing this on I have about 100.000 files. So obviously you want to organize them some how so that you can find anything at all. So the files are organized into directories that can contain further directories and/or files. This is called hierarchical file system. (Pity that the QDOS that Bill bought back in the old days and that later became known as MS-DOS did not have a file hierarchy, not to mention long file names. If he had bought something deacent, like OS-9, boy would we all have been saved from a lot of headache.)
Unlike Windows, Linux uses no drive letters, such as 'C:' to refer to disks. In Linux everything is a directory. The top most directory is equivalent to the drive letter. In Cygwin, which pretends to be a Linux but runs on Windows, the windows 'C:' disk is refered to as directory '/cygdrive/c'. And the root of Cygwin directory tree looking from Windows side is (usually) 'C:/cygwin', under which you find 'home', 'bin', 'etc' ...
File and directory names can contain all kinds of letters but it is best to use names that only use english lowercase letters and digits plus maybe dashes and underlines. Especially you should avoid names with spaces (' ') in them. They will make your life miserable. Names that begin with period '.' are not usually visible in directory listings which is why the many operating system tools store configuration data into files beginning with '.'. Or actually it is the other way round, because tools store info into those files they are hidden.
Names are case sensitive in Linux whereas in Windows they are not. This can be very confusing if you are testing something, say a web page, in Windows and it works but does not work when you upload it to the server which is most likely a Linux box.
Each session has current directory assosiated with it. What this means in Finglish is that if you do not specify (in a command) otherwise it is assumed that you are refering to the current directory. By having multiple sessions it is easy to work in multiple directories because in effect every terminal window has its own current directory.
Here are the most common commands that are used to work with (current) directories.
There are relative and absolute path names. A relative pathname begins with a letter and it refers to a file or directory that is in the current directory or a subdirectory of current directory. An absolute path name begins with a slash and specifies the full 'path' from the highest level of the file system to the file/directory in question.
The current directory can be referenced as '.' and the parent directory of the current directory as './..' and so on.
Every user, based on their username, has a special directory called home directory. Immediatelly after login current director is set to your home directory. The home directory can be referenced as '~' in a path.
The asterisk ('*') can be used as a wild card in most situations when specifying files on a command line.
There are conventions in Linux world on how to organize the file hierarchy. There is nothing to enforce these conventions but most system are organized along these lines.
Here are some often used directories:
In DOS/Windows you can store commands into batch files that end with the name extension '.BAT' which allows you to execute those commands by typing the file name. In effect creating more commands out of existing commands. This is often called scripting and the the files that contain commands are called , you guessed it, scripts.
In Linux any text file is a potential script. When you type a file name the shell checks if the file is marked executable and if it is it will execute the commands in that file. A nice example of this is the way GNU tools are configured. You usually configure them by going to the directory that contains the source code and type
To make a file executable (for everybody) you use the chmod command, like this:
To see if a file is executable and other stuff use:
Simply (very simply) put
Anyway, here are some usefull grep's :
Counter intuitively and unfortunatelly the following DOES NOT work:
Following gibberish executes
This is a way too complex subject, but amazingly a lot of GNU based Free software actually can be installed and compiled by going to top level directory of the source distribution and entering following:
and executed (run) with:
thats all folks,
cheers Kusti
Unlike Windows, Linux uses no drive letters, such as 'C:' to refer to disks. In Linux everything is a directory. The top most directory is equivalent to the drive letter. In Cygwin, which pretends to be a Linux but runs on Windows, the windows 'C:' disk is refered to as directory '/cygdrive/c'. And the root of Cygwin directory tree looking from Windows side is (usually) 'C:/cygwin', under which you find 'home', 'bin', 'etc' ...
File and directory names can contain all kinds of letters but it is best to use names that only use english lowercase letters and digits plus maybe dashes and underlines. Especially you should avoid names with spaces (' ') in them. They will make your life miserable. Names that begin with period '.' are not usually visible in directory listings which is why the many operating system tools store configuration data into files beginning with '.'. Or actually it is the other way round, because tools store info into those files they are hidden.
Names are case sensitive in Linux whereas in Windows they are not. This can be very confusing if you are testing something, say a web page, in Windows and it works but does not work when you upload it to the server which is most likely a Linux box.
Each session has current directory assosiated with it. What this means in Finglish is that if you do not specify (in a command) otherwise it is assumed that you are refering to the current directory. By having multiple sessions it is easy to work in multiple directories because in effect every terminal window has its own current directory.
Here are the most common commands that are used to work with (current) directories.
pwd Show the directory you are currently in
cd dirname Change current directory to 'dirname' directory
cd .. Move to (make current) the parent of your current directory
cd ~ Move to your home directory
mkdir dirname Make a new directory named 'dirname'
rmdir dirname Remove a directory named 'dirname' Note: All files must first be deleted
Paths
Files and directories are referd to by path names.There are relative and absolute path names. A relative pathname begins with a letter and it refers to a file or directory that is in the current directory or a subdirectory of current directory. An absolute path name begins with a slash and specifies the full 'path' from the highest level of the file system to the file/directory in question.
The current directory can be referenced as '.' and the parent directory of the current directory as './..' and so on.
Every user, based on their username, has a special directory called home directory. Immediatelly after login current director is set to your home directory. The home directory can be referenced as '~' in a path.
The asterisk ('*') can be used as a wild card in most situations when specifying files on a command line.
There are conventions in Linux world on how to organize the file hierarchy. There is nothing to enforce these conventions but most system are organized along these lines.
Here are some often used directories:
/bin for binary (executables),
/dev for device files,
/etc for admin and personal information,
/tmp for temporary files, and
/home for home directories of individual users
Working with Files
Here are the most common commands that you need to work with files.
ls List the contents of the current directory
ls -l Give a long-form listing, with lots of information about each file and directory
ls -a Do a normal ls, but include all files whose names begin with a period (hidden files)
ls -F Do a normal ls, but mark all executable files with a *, and all directories with a /
ls -laF Do all of the above
ls -R1 List all files in current directory and all sub directories, each file on its own line
mv oldname newname Move (or, more precisely, rename) the file oldname to the file newname.
mv file dirname Move a file into directory named dirname.
cp filename newname Copy the file filename to the file newname.
cp filename dirname Copy a file into a directory.
rm file Remove (that is, delete) a file. There's no "undelete" command, so be careful.
cat file "Concatenate" the file--that is, print its entire contents to the screen, all at once.
For big files, use more file.
Getting Help
Most commands list a short description of their usage and possible argumenst i.e. parameters when given the parameter --help. For example:
ls --helpYou can also access a lot of documention for each command with the man command. This lists the manual pages for a given command page by page. Press 'space' to get to the next page, press 'q' to quit reading. For example:
man ls
Commands, Scripts and Shells
In Linux all commands are actually small programs that you run. You run them by typing their names to the command interpreter which the in-crowd calls 'shell'. There are quite a few shells around but most resemble each other. In Cygwin the shell (which is just another program) is called 'bash'.In DOS/Windows you can store commands into batch files that end with the name extension '.BAT' which allows you to execute those commands by typing the file name. In effect creating more commands out of existing commands. This is often called scripting and the the files that contain commands are called , you guessed it, scripts.
In Linux any text file is a potential script. When you type a file name the shell checks if the file is marked executable and if it is it will execute the commands in that file. A nice example of this is the way GNU tools are configured. You usually configure them by going to the directory that contains the source code and type
configure somethingThe 'configure' is actually a script that contains shell commands to do the configuration. While I'm on the subject: the 'configure' script usually creates a Makefile, which is used to build the tools. More on that later.
To make a file executable (for everybody) you use the chmod command, like this:
chmod a+x filenameInstead of 'x' you could add (with '+') or remove (with '-') read ('r') or write ('w') access to a given file.
To see if a file is executable and other stuff use:
ls -l filename
Pipes and more (or less)
Most Linux commands read in some text or parameters and then produce and output some text. It is possible to redirect both the input and output of a program. You do that with '<inputfilename' for input and with '>outputfilename' for output. For example, if you want to create a list of all files in your current directory and store it into a text file use:ls >mydirlistfile.txt
It also possible to direct the output of one program to the input of another. This is called piping and is effected with '|'. Very often used with 'more' command, (or maybe with 'less' command as 'more' does not seem to be available in Cygwin) , which is a command that just outputs what it inputs, but does it page by page so that you have a chance to read it. For example if you invoke the 'ls' command recursively to see all the files in your current directory or any subdirectory you will be creating a looooooooong listing, which will whirl by at speed of light. To cut it to readable chunks use:ls -R | less
One of the most common uses for piping is to use it to feed input to grep
command.Using Grep as a Filter
Lots of people lovegrep
, but I'm not one of them. Everytime I need it I need to look it up from the web. So I frequently don't use it, hence I don't learn and need look it up everytime I need it...Simply (very simply) put
grep
grep reads the input and passes through lines that match the criteria you give to the grep
command.Anyway, here are some usefull grep's :
grep ".java" Pass through every line that contains the string ".java" grep ".java\|.c" Pass through every line that contains either the string ".java" or ".c" grep -v ".class" Pass through every line that does NOT contain either the string ".java"Following is a way to list all '*.java' files and the directories in which they are starting recursively from current directory:
ls -R1 | grep -e ".java\|/"
Finding (in) files
To find a file containing a certain word use:
grep -n void example.hFinds word 'void' in file 'example.h'
grep -n 'int x' *.h
Finds fragment 'int x' in all '.*' files in current directorygrep -Rn 'func' *.h
Finds word 'func' in allfiles in current directory or any sub directorygrep -Rn 'b*lean' filename
Finds word 'b*lean' where '*' stands for anythingCounter intuitively and unfortunatelly the following DOES NOT work:
grep -Rn void *.hIf you want search all files of certain type in current directory and sub directories you need to use:
find . -name "*.c" -exec grep -l -n 'this' {} \;Finds 'this' in any '*.c' file.
Following gibberish executes
wc<
(word count) command on each '*.java' file in current directory and any subdirectory and stores the results to a text file 'wc.txt'. Handly for collecting statistics of you lates software project.
find . -name "*.java" -exec wc {} \; >wc.txt
Unpacking Files
A lot of things in the Linux world are distributed as tar (aka tarball) files which are compressed withgzip
or gzip2
.Most Linux/Free software is delivered in source code format.tar xfj collection1.bz2
Uncompresses and then extract all the files from the arcieve 'collection1'tar xfz collection1.tar.gz
Uncompresses and then extract all the files from the arcieve 'collection2'
make
ing Programs
This is a way too complex subject, but amazingly a lot of GNU based Free software actually can be installed and compiled by going to top level directory of the source distribution and entering following:Note that many packages can take hours to compile! If you do not see any output, that is a good sign, in the best tradition of "no news is good news" most Linux tool produce no output when things are going smoothly!./configure
make
make install
Compiling Software
This is even more complex subject but a simple hello word program such as
#include <stdio.h>
int
main (void)
{
printf ("Hello, world!\n");
return 0;
}
can be compiled with:cc hello.c -o hello
and executed (run) with:
./hello
thats all folks,
cheers Kusti
Miscallenious
To create a check an md5sum index of files (to facilitate dumplicate file finding):
find DIRECTORY -type f -exec md5sum "{}" \; | sort >/tmp/index
find . -name "*.java" -exec grep -v '^[[:space:]]*$' $ {} \; | wc
Комментариев нет:
Отправить комментарий
ваше мнение...