> For the complete documentation index, see [llms.txt](https://dev117uday.gitbook.io/notes-md/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://dev117uday.gitbook.io/notes-md/linux/linux-commands-part-1-4.md).

# Linux Commands Part 1 - 4

Tags: linux-com-book

## Introductory Commands

* `#` before terminal means super-user
* `date` to print date and time
  * `Sat Jun 18 12:01:17 AM IST 2022`
* `cal` to print calendar
* `df` to print disk drive and usage
* `free` to print memory
* `pwd` print working directory
* `Absolute PathName` : begins with root directory and follows the tree branch by branch till the desired folder
* `Relative PathName` : starts from the working directory
* `cd` to change directory
  * `cd -` to revert to previous directory
  * `cd ~username` : to change directory to another user
* ls : to list directories

  * `ls directory_1 directory_2` : to print list or multiple directories

  ```java
  ➜  ~ ls programm software
  programm:
  azure-dev-hackathon  inbox-app            
  configs              learning-springboot  
  demo                 playground           

  software:
  jetbrains-toolbox  tor-browser_en-US
  ```

  * `ls -l` : for more details
  * `ls -lt` : to list sorted acc. date modifed
    * `—reverse` : to reverse the sort

  ![List of options of ls command](https://473782116-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MlTUR0kU-pvc2XyvuDJ%2Fuploads%2Fgit-blob-690ba4d9e87b8035d625318da3bcbcdc5b953fc9%2FUntitled.png?alt=media)

Long Listing Details

![details of fields printing long list](https://473782116-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MlTUR0kU-pvc2XyvuDJ%2Fuploads%2Fgit-blob-2c112a0b67e026bbc8c10fde496b75dc8cf14d42%2FUntitled%201.png?alt=media)

* `file filename` : to print a brief description of the file’s contents
* `less` : to view content of file

## File Structure

![Linux file structure part 1](https://473782116-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MlTUR0kU-pvc2XyvuDJ%2Fuploads%2Fgit-blob-bbd6dda95aafe9fdb3e7e7f4843ef52d368fa3fd%2FUntitled%202.png?alt=media)

![Linux file structure part 2](https://473782116-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MlTUR0kU-pvc2XyvuDJ%2Fuploads%2Fgit-blob-ab7dc68df2abf790b6bbcc29acebb12fa232c38d%2FUntitled%203.png?alt=media)

![Linux file structure part 3](https://473782116-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MlTUR0kU-pvc2XyvuDJ%2Fuploads%2Fgit-blob-e31727ce3cf6def295cf9d806d31afba0e9c090d%2FUntitled%204.png?alt=media)

![Linux file structure part 4](https://473782116-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MlTUR0kU-pvc2XyvuDJ%2Fuploads%2Fgit-blob-2cce48493031003c56f7ed0fc7d5435054d00eac%2FUntitled%205.png?alt=media)

![Linux file structure part 5](https://473782116-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MlTUR0kU-pvc2XyvuDJ%2Fuploads%2Fgit-blob-4c307f62ad04e532de7e5b6bcd28647eff645cba%2FUntitled%206.png?alt=media)

## Symbolic Links | Soft Link | Symlink

* symbolic links are like aliases or another name for the file

```java
lrwxrwxrwx 1 root root
11 2018-08-11 07:34 libc.so.6 ***->*** libc-2.6.so
```

**Why they are useful ?**

* Imagine we have a file with name `foo` . Whenever we update it we, change its name to foo-date-time, but the programs/user referencing it wouldst know that the name has change. To tackle this problem, we create a symlink of file foo → foo-date-time and the programs/user user only reference the symlink.
* So even if we change the name of file foo, it wont affect the programs as they would be referencing the symlink.

## Manipulating Files and Directories

* to create new directories
  * `mkdir dir_name`
* to create multiple directories
  * `mkdir dir1 dir2 dir3`
* to copy file to another directory
  * `cp item directory`
  * if `cp item1 item2` then content of item2 is replace with item1
* use `-a` file while copying to copy the attributes as well

![options for cp command](https://473782116-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MlTUR0kU-pvc2XyvuDJ%2Fuploads%2Fgit-blob-bf4a3a5519e44316e5da0d114f71c1e832e2025a%2FUntitled%207.png?alt=media)

* The `mv` command performs both file moving and file renaming, depending on how it is used
* to move item to another directory `mv item... directory`
* to delete item `rm item`

![options for rm command](https://473782116-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MlTUR0kU-pvc2XyvuDJ%2Fuploads%2Fgit-blob-976f68e638f08a6cebd2884c9539e1ffde6d88fb%2FUntitled%208.png?alt=media)

## Hard Links vs Soft Links

### Hard Link

* Hard links are the original Unix way of creating links
* Every file has a single hard link that gives the file its name
* Limitations
  * A hard link cannot reference a file outside its own file system
  * A hard link may not reference a directory
* A hard link is indistinguishable from the file itself

```bash
// for hard link
-> ln file.txt link

-> ls
-rw-r--r--. 1 udayyadav udayyadav  12 Jun 20 23:06 file.txt
-rw-r--r--. 2 udayyadav udayyadav  12 Jun 20 23:06 link1
```

### Soft Link

* They work by creating a special type of file that contains a text pointer to the referenced file or directory
* They are like windows shortcuts
* If file is deleted, then link points to nothing, it is broken

```bash
// for soft link
-> ln -s file.txt link

-> ls
lrwxrwxrwx. 1 udayyadav udayyadav   9 Jun 21 07:59 link2 -> file2.txt
```

## WildCards

![different wildcards present](https://473782116-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MlTUR0kU-pvc2XyvuDJ%2Fuploads%2Fgit-blob-e4d9a3f20aa6aef72fa86ecf0ccb306d95d8edf4%2FUntitled%209.png?alt=media)

![matching with different characters](https://473782116-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MlTUR0kU-pvc2XyvuDJ%2Fuploads%2Fgit-blob-a46cc42217100c681ab7d2138ab0d45ee3953c09%2FUntitled%2010.png?alt=media)

![wildcard example](https://473782116-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MlTUR0kU-pvc2XyvuDJ%2Fuploads%2Fgit-blob-d869b989c138eb02cb86b5b7836c878f3bd80842%2FUntitled%2011.png?alt=media)
