| 361 | === Working with Files === |
| 362 | |
| 363 | Files on Linux (or any Unix) are case sensitive. This means that FILE1 is different from file1, and /etc/hosts is different from /etc/Hosts (the latter one does not exist on a typical Linux computer). In Linux everything is considered as a file even a directory is a special kind of file. A small overview of some basic and important file handling commands |
| 364 | |
| 365 | - '''file''': The file utility determines the file type. Linux does not use extensions to determine the file type. The command line does not care whether a file ends in .txt or .pdf. As a system administrator, you should use the file command to determine the file type. |
| 366 | - '''cp''': To copy a file, use cp with a source and a target argument. |
| 367 | * '''cp -r''': To copy complete directories, use cp -r (the -r option forces recursive copying of all files in all subdirectories). |
| 368 | * '''cp -i''': To prevent cp from overwriting existing files, use the -i (for interactive) option. |
| 369 | - '''mv''': Use to rename a file or to move the file to another directory. |
| 370 | - '''rm''': Use to remove files |
| 371 | *'''rm -i''':To prevent yourself from accidentally removing a file. |
| 372 | *'''rm -rf''':'''rm -r''' will not remove non-empty directories. However rm accepts several options that will allow you to remove any directory. The rm -rf will erase anything |
| 373 | '''Extremely dangerous Command''' |
| 374 | {{{ |
| 375 | When you are logged on as root, be very careful with rm -rf (the f means force and the r means recursive) since being root implies |
| 376 | that permissions don't apply to you. You can literally erase your entire file system by accident. |
| 377 | }}} |
| 378 | - '''less''':The less command is useful for displaying files that take up more than one screen |
| 379 | - '''head/tail''':You can use head to display the first ten lines of a file. and tail to display the last ten lines of a file. you can use both commands with '''-n''' and specify the number of lines |
| 380 | - '''cat''':The cat command is one of the most universal tools, yet all it does is copy standard input to standard output. |
| 381 | |
| 382 | === Exercise === |
| 383 | |
| 384 | - Create numbers.txt (Containing ten lines of numbers one to ten)file with cat command. |
| 385 | {{{ |
| 386 | $ cat > numbers.txt |
| 387 | }}} |
| 388 | Add the lines with enter key at the end. After inserting all the lines press Ctrl + D |
| 389 | {{{ |
| 390 | one |
| 391 | two |
| 392 | . . |
| 393 | ten |
| 394 | }}} |
| 395 | - View the file you created using less and cat |
| 396 | '''cat''' |
| 397 | {{{ |
| 398 | $ cat numbers.txt |
| 399 | one |
| 400 | two |
| 401 | three |
| 402 | four |
| 403 | five |
| 404 | six |
| 405 | seven |
| 406 | eight |
| 407 | nine |
| 408 | ten |
| 409 | }}} |
| 410 | '''less''' |
| 411 | {{{ |
| 412 | $ less numbers.txt |
| 413 | }}} |
| 414 | Press '''q''' to exit from less |
| 415 | - View first four lines and last four lines using head and tail commands |
| 416 | {{{ |
| 417 | $ head -n 4 numbers.txt |
| 418 | one |
| 419 | two |
| 420 | three |
| 421 | four |
| 422 | $ tail -n 4 numbers.txt |
| 423 | seven |
| 424 | eight |
| 425 | nine |
| 426 | ten |
| 427 | }}} |
| 428 | |
| 429 | - Create another file numbers2.txt(with lines contain eleven to fifteen) |
| 430 | {{{ |
| 431 | $ cat numbers2.txt |
| 432 | eleven |
| 433 | twevelve |
| 434 | thirteen |
| 435 | fourteen |
| 436 | fifteen |
| 437 | }}} |
| 438 | - Combine numbers.txt and numbers2.txt and create numbers3.txt. and view the file. |
| 439 | {{{ |
| 440 | $ cat numbers.txt numbers2.txt > numbers3.txt |
| 441 | $ cat numbers3.txt |
| 442 | one |
| 443 | two |
| 444 | three |
| 445 | four |
| 446 | five |
| 447 | six |
| 448 | seven |
| 449 | eight |
| 450 | nine |
| 451 | ten |
| 452 | eleven |
| 453 | twevelve |
| 454 | thirteen |
| 455 | fourteen |
| 456 | fifteen |
| 457 | }}} |
| 458 | - Check the file format of the newly created file |
| 459 | {{{ |
| 460 | $ file numbers3.txt |
| 461 | numbers3.txt: ASCII text |
| 462 | }}} |
| 463 | |
| 464 | - Copy all the created files to the test directory. and verify |
| 465 | {{{ |
| 466 | $ cp numbers.txt numbers2.txt numbers3.txt test/ |
| 467 | $ cd test |
| 468 | $ ls |
| 469 | numbers2.txt numbers3.txt numbers.txt one |
| 470 | }}} |
| 471 | |
| 472 | - Make a copy of test directory as newtest and rename it to numbers |
| 473 | {{{ |
| 474 | $ cp -r test/ newtest |
| 475 | $ mv newtest/ numbers |
| 476 | }}} |
| 477 | |
| 478 | - Delete the test directory |
| 479 | {{{ |
| 480 | $ rm -rf test |
| 481 | }}} |
| 482 | |
| 483 | |