68 lines
2.2 KiB
Markdown
68 lines
2.2 KiB
Markdown
---
|
|
title: 'Useful Commands'
|
|
date: 2023-09-26T12:04:05-04:00
|
|
tags: ["shell", "script", "command line"]
|
|
author: "Me"
|
|
endbleGitInfo: true
|
|
showToc: true
|
|
TocOpen: false
|
|
draft: true
|
|
hidemeta: false
|
|
description: 'A collection and living document of useful shell commands that I use.'
|
|
disableHLJS: true # to disable highlightjs
|
|
disableShare: false
|
|
disableHLJS: false
|
|
hideSummary: false
|
|
searchHidden: true
|
|
ShowReadingTime: true
|
|
ShowBreadCrumbs: true
|
|
ShowPostNavLinks: true
|
|
ShowWordCount: true
|
|
ShowRssButtonInSectionTermList: true
|
|
UseHugoToc: true
|
|
cover:
|
|
image: "useful_commands.png" # image path/url
|
|
alt: "<alt text>" # alt text
|
|
caption: "<text>" # display caption under cover
|
|
relative: false # when using page bundles set this to true
|
|
hidden: true # only hide on current single page
|
|
---
|
|
|
|
This post will act a bit like a living document of all the useful commands and short scripts that I use regularly. Some will
|
|
be as simple as `ls | wc -l` which counts the number of files in your current working directory, and others will be better
|
|
suited to small scripts. As this grows, I will hopefully break these into more logical categories.
|
|
|
|
## Find by File type
|
|
|
|
This command will find every file in your current working directory by the specified file extension.
|
|
|
|
```bash
|
|
find . -type f -name '*.txt'
|
|
```
|
|
|
|
As a bonus, if you wanted find everything _except_ that file extension, you can add a `!` before name. In other words:
|
|
|
|
```bash
|
|
find . type -f ! -name '*.txt'
|
|
```
|
|
|
|
I usually use commands like this to clean up files that I don't need, be it file extensions I don't need anymore, or anything
|
|
with a specific word in its file name, so adding `-delete` at the end of the command lets me delete those files.
|
|
|
|
```bash
|
|
find . type -f -name '*.txt' -delete
|
|
```
|
|
|
|
## List Files & Dir +
|
|
|
|
Using `ls` is a _key_ command for anyone that works within a command line. If you've used `ls` before, then you've also
|
|
likely used or created an alias for `ls -la` which will display the files, their user:group ownership, permissions
|
|
(`drwxrwxrwx`) and dates created + modified.
|
|
|
|
What if you just want to found the number of files? Not measuring disk usage or free space, but the equivalent of `len` for
|
|
an array, the array being your current working directory. Enter `wc`!
|
|
|
|
```bash
|
|
ls | wc- l
|
|
```
|