Difference between revisions of "Linux commands"
(move memstick to devices) |
(→Search for file content recursively: simpler and more useful example) |
||
Line 61: | Line 61: | ||
=== Search for file content recursively === | === Search for file content recursively === | ||
− | Here | + | Here's an example looking for a phrase within a specific file type recursively through a directory structure, and printing the file names and line numbers of the resulting matches. |
− | {{code|<bash> | + | {{code|<bash>grep -rn "Foo" *.php</bash>}} |
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | </bash>}} | ||
There are other tips at [http://stackoverflow.com/questions/316590/how-do-you-use-cat-recursively stackoverflow.com]. | There are other tips at [http://stackoverflow.com/questions/316590/how-do-you-use-cat-recursively stackoverflow.com]. |
Revision as of 15:05, 12 May 2015
Contents
- 1 Ubuntu package management
- 2 SSH & SCP
- 3 Files & Devices
- 3.1 Get the size of a directory and its contents
- 3.2 Search for file content recursively
- 3.3 Search for file content recursively and tar
- 3.4 Search for file content recursively and long list
- 3.5 Count occurrences of a word in a file or files
- 3.6 Search and replace content in files
- 3.7 Mass renaming
- 3.8 Compare two directory structures for differences
- 3.9 Mount a .iso
- 3.10 Mount a USB stick
- 4 Image manipulation
- 5 Audio conversion
- 6 Video manipulation
- 7 System monitoring
- 8 Network commands
- 8.1 Restart the network after changing configuration
- 8.2 List all the listening sockets and their ports and programs
- 8.3 Get current default gateway
- 8.4 Release DHCP lease
- 8.5 Obtain a new DHCP lease
- 8.6 Scan a local subnet for active IP addresses
- 8.7 Get the MAC address and hostname of an IP on the current subnet
- 9 Internet
- 10 Port forwarding
- 11 General
- 12 See also
Ubuntu package management
Searching for an installed package
Use dpkg and grep;
will list all packages with java in the name
Searching for installable packages
apt-cache performs a variety of operations on APT´s package cache
lists all packages in the cache with java in the name that are installable
SSH & SCP
tar pipe
Example of sending a file to a remote server:
Example of retrieving a file from a remote server:
Note using port forwarding for commonly accessed sites you can define non standard ports in your .ssh/config
so you never
need to explicitly state them above.
Recovering from an interrupted transfer
This ability is included natively in Linux and used to be very useful for splitting large backups up so they could fit onto small media such as floppy disks. But there's one time when it's very useful nowadays too which is when a large file transfer gets interupted and there's no option for continuation such as when using SCP.
When this happens, go onto the target machine, check how many bytes were transferred of the target file and rename it to "xaa". Then on the local machine, do the following command to split the source file into two parts, the first being of the size of the number of bytes that have already been transferred (in this example 1234567 bytes).
The resulting files are called "xaa" and "xab", and the latter is the containing the remaining content that needs to be transferred to the target server. Once you've transferred it across, you can then join the two files (remember you renamed the first part to "xaa" so there's no need to transfer that) together using cat as follows, and then remove all the xa* files from noth source and target servers.
Files & Devices
Get the size of a directory and its contents
Use this more specific version to find the size of a users Maildir folder:
Search for file content recursively
Here's an example looking for a phrase within a specific file type recursively through a directory structure, and printing the file names and line numbers of the resulting matches.
There are other tips at stackoverflow.com.
Search for file content recursively and tar
Search for file content recursively and long list
Count occurrences of a word in a file or files
Search and replace content in files
You could also use find and sed, but I find that this little line of perl works nicely.
- -e means execute the following line of code.
- -i means edit in-place
- -w write warnings
- -p loop
EXTS="7z t7z"
Mass renaming
mmv is a mass move/copy/renaming tool that uses standard wildcards to perform its functions. According to the manual the “;” wildcard is useful for matching files at any depth in the directory tree (ie it will go below the current directory, recursively).
- Example
The first pattern matches anything with a “.JPG” and renames each file (the “#1” matches the first wildcard) to “.jpg”. Each time you use a \(wildcard) you can use a #x to get that wildcard. Where x is a positive number starting at 1. Copied off: http://tldp.org/LDP/GNU-Linux-Tools-Summary/html/mass-rename.html
Compare two directory structures for differences
This is very handy if you need to know if two directory structures are the same or not including file content. It uses the diff command with the r switch to work recursively and the x switch to allow skipping of the .svn directories.
Mount a .iso
See this HOWTO
Mount a USB stick
If you don't know the device name of the stick, plug it in and find it with the following:
Then mount the first partition,
Image manipulation
Resizing JPG's and changing quality setting
The first line shows how to reduce and image to 25% and quality to 50% adding "_resized" to the results filename. The second command uses Perl to apply this same command to all JPG's in the current directory.
Apply an opaque background of a specified colour to a directory of transparent PNG's
- This command requires ImageMagick to be installed
- It loops through all PNG's in the CWD and puts them in a directory called processed which must exist
Audio conversion
The following converts a .wav file to an mp3:
To do a whole directory you could do this:
Video manipulation
Use the following commands to extract a small snippet out of a video (the -ar switch is only needed for outputting to flv I think).
See also
- Converting_microarray_images for bash/imagemagick image file manipulation
System monitoring
List the top 10 memory consuming processes
List the top 10 CPU consuming processes
Network commands
Restart the network after changing configuration
List all the listening sockets and their ports and programs
Get current default gateway
The default gateway is on the last line, it should have the U and G flags set
Release DHCP lease
Obtain a new DHCP lease
Scan a local subnet for active IP addresses
Get the MAC address and hostname of an IP on the current subnet
Internet
Download a copy of an entire site
This will download an entire site, if the site is already downloaded, then only newer files are transferred.
Port forwarding
Port forwarding allows a remote client to gain access to a network so an intranet can be accessed.
Then point your webbrowsers proxy server to appserver:[PORT] and access the intranet etc.
To subvert a firewalled environment where outgoing ssh is allowed.
Then point your web browser to a SOCKS proxy @ localhost:9000
Further it's possible to get ssh through a web proxy using corkscrew.
General
Add and remove startup items