Blame

ae6cb8 Arpan S. 2024-03-29 11:28:56 1
# Standard Linux Commands
460022 Arpan S. 2024-03-29 11:41:37 2
3
This page contains some standard linux commands that are available for use across all linux systems but are easy to forget that they exist or using them can get confusing sometimes.
4
ca2477 Arpan S. 2024-03-29 20:45:25 5
#### `wc` Command
845283 Arpan S. 2024-03-29 21:30:48 6
This command is used to count things in linux and supports piping. It can be used for multiple purposes as shown below.
ca2477 Arpan S. 2024-03-29 20:45:25 7
8
Syntax:
9
```
10
wc OPTION... [FILE]...
11
```
342d25 Arpan S. 2024-03-29 21:28:17 12
###### Options
ca2477 Arpan S. 2024-03-29 20:45:25 13
342d25 Arpan S. 2024-03-29 21:28:17 14
- `-l` / `--lines` - Print number of lines.
15
- `-w` / `--words` - Print number of words.
16
- `-m` / `--chars` - Print the number of characters.
17
- `-c` / `--bytes` - Print the number of bytes.
18
- `-L` / `--max-line-length` - Print the length of the longest line.
19
20
Default Output:
21
22
```
23
# wc fileA
24
20 30 60 fileA
25
```
26
Here, _20_ is the number of lines, _30_ is the number of words & _60_ is the number of characters in the file.
27
28
Generic uses:
29
30
Standard usage (Pipe Back)
31
```
32
wc < fileA
33
```
34
35
Multiple Files
36
```
37
# wc fileA fileB
38
```
39
_Output:_
40
```
41
20 40 60 fileA
42
60 80 100 fileB
43
80 120 160 total
44
#
45
```
46
47
Number of words (Piped Output)
48
```
49
cat /etc/shadow | wc -w
50
```
51
52
Number of lines (Piped Output)
53
```
54
cat /etc/shadow | wc -l
55
```
56
57
###### How to count number of lines in a file using `wc`
58
```
59
wc -l /path/to/file
60
```
61
62
###### How to count number of words in a file using `wc`
63
```
64
wc -w /path/to/file
65
```
66
67
###### How to count number of lines output by a command using `wc`
68
```
69
example_command | wc -l
70
```
71
72
###### How to count number of files in current directory using `wc`
73
```
74
find . -type f | wc -l
75
```
76
ca2477 Arpan S. 2024-03-29 20:45:25 77
b7eea6 Arpan S. 2024-03-29 12:51:07 78
#### `ln` Command - How to create symbolic links / sys links in Linux.
79
80
##### General Explaination and use-case
81
82
There are two types of system links in linux:
2932d9 Arpan S. 2024-03-29 13:00:48 83
1. Hard Links: Its is like having an alias name to an existing file. It links _(better term: syncs)_ two or more file names with the same [inode](https://en.wikipedia.org/wiki/Inode). **You can only create hardlinks for files & folders on the same filesystem or partition**. One file can have multiple hardlinks.
b7eea6 Arpan S. 2024-03-29 12:51:07 84
2. Soft Links: A soft link can be related to a shortcut in windows, it allows a file to be accessed from one directory to other without having to keep 2 copies of it. Soft Links can be created across different filesystems and partitions.
85
86
##### How to use ln command
87
88
By default `ln` command creates **Hard Links**, To make **Soft Links**, We can use the `-s` _(`--symbolic`)_ flag
89
2932d9 Arpan S. 2024-03-29 13:00:48 90
Syntax:
b7eea6 Arpan S. 2024-03-29 12:51:07 91
```python
92
ln -s [OPTIONS] SOURCE_FILE DESTINATION_LINK
93
```
94
`DESTINATION_LINK` is optional, If that is not specified then it will create a system link in the current working directory with the same name.
95
96
::: success
97
The `ln` command provides no output if ran successfully.
98
:::
99
100
>| # Examples
101
>| SysLink a Normal file
102
>| ```
103
>| ln -s file1 file2
104
>| ```
105
>| SysLink a Normal file in different directories
106
>| ```
107
>| ln -s /root/file1 /var/www/html/file1
108
>| ```
109
>| SysLink a Normal Directory to another
110
>|
111
>| ```ln -s /root/downloads /downloads```
112
113
###### **How to see if a file is a System Link:**
114
To see if a file is already a system link/sysLink/symbolic link, We can use the `ls` command with the `-l` flag. Syntax:
115
```
116
# ls -l syslinked_file
117
lrwxrwxrwx 1 root root 9 Nov 22 2004 /root/og_file -> syslinked_file
118
#
119
```
120
121
###### **How to overwrite a System Link:**
122
To overwrite an existing sysLink when the `DESTINATION_FILE` already exists as a sysLink, we can use the -f flag. Syntax Below:
123
```
124
ln -sf new-file.conf existing-syslink.conf
125
```
126
127
###### **How to remove/delete a System Link:**
845283 Arpan S. 2024-03-29 21:30:48 128
To delete/remove a sysLink we can use the `rm` or `unlink` command. Please note that both commands work exactly the same._[Refrence](https://linuxize.com/post/how-to-remove-symbolic-links-in-linux/)_
b7eea6 Arpan S. 2024-03-29 12:51:07 129
130
::: danger
131
Removing or Moving away the Source file will cause the sysLink to break and it won't work anymore. its is best advised to remove any broken sysLinks.
132
:::
133
134
Syntax:
135
136
`unlink` Command
137
```
138
unlink symlink_to_remove
139
```
140
`rm` Command
141
```
142
rm symlink_to_remove
143
```
0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9