|
| 1 | +============================================================ |
| 2 | +Linux Basics_DAY-2: Working with Files and Directories |
| 3 | +============================================================ |
| 4 | +1.Create: File/Directory |
| 5 | +------------------------------------------------------------ |
| 6 | +Creation of files in different ways: |
| 7 | +------------------------------------------------------------ |
| 8 | +We can use touch to create an empty file and mkdir to create a directory. |
| 9 | +Ex: touch <fine_name> |
| 10 | +EX: touch fileA.txt fileB.txt fileC.txt |
| 11 | +EX: touch file{1..5}.txt |
| 12 | +Ex: > <filename> ('>' ==> redirection operator) |
| 13 | +Ex: cat > <filename> and press Ctrl+D to save. (cat==>Concatenates and displays files:File handling command.) |
| 14 | +Ex: echo "" > <filename> (echo ==> Displays text on the terminal:Output command) |
| 15 | +Ex: printf "" > <filename> (printf ==> Formats and prints output:Formatting and output command) |
| 16 | +-------------------------------------------------------------- |
| 17 | +Creating files in different ways: |
| 18 | +mkdir -p Storage/local/user/documents |
| 19 | +touch ./Storage/local/user/userinfo.txt |
| 20 | +mkdir -p Storage/local/backups |
| 21 | +touch ./Storage/local/backups/logsinfo.txt |
| 22 | +-------------------------------------------------------------- |
| 23 | +We can look at the whole structure after creating the parent directories with the tool tree. |
| 24 | +Ex: tree . |
| 25 | +Ex: tree -L 2 |
| 26 | +Ex: tree -d |
| 27 | +--------------------------------------------------------------- |
| 28 | +Real-time Scenarios: |
| 29 | +Creating a File with System Information: |
| 30 | +(Saving system uptime and load average to a file) |
| 31 | +uptime > /var/log/system_status.log |
| 32 | + |
| 33 | +Redirecting Command Output to Multiple Files: |
| 34 | +(Saving a list of active users and processes to separate files) |
| 35 | +who > /var/log/active_users.log; ps aux > /var/log/processes.log |
| 36 | + |
| 37 | +Redirecting the Output of a System Command: |
| 38 | +(Saving the list of installed packages to a file) |
| 39 | +rpm -qa > /var/log/installed_packages.log |
| 40 | + |
| 41 | +============================================================== |
| 42 | +Copy: 'cp' command in Linux is used to copy files and directories |
| 43 | +=============================================================== |
| 44 | +Ex: cp file1.txt file2.txt |
| 45 | +Ex: cp ./Storage/local/backups/logsinfo.txt ./Storage/local/user/ |
| 46 | +Ex: cp -r ./Storage/local/user/ ./Storage/local/backups/ |
| 47 | +Ex: cp -u logsinfo.txt ./Storage/local/backups/logsinfo.txt |
| 48 | +============================================================ |
| 49 | +Move: 'mv' command in Linux is used for moving or renaming files and directories. |
| 50 | +============================================================ |
| 51 | +Syntax: mv <file/directory> <renamed file/directory> |
| 52 | + |
| 53 | +Ex: mv samplefile.txt filesample.txt |
| 54 | +Ex: mv /var/log/active_users.log ./Storage/local/user/ |
| 55 | + |
| 56 | + |
| 57 | + |
0 commit comments