kiophen

Linux Tips

After having had a lot of time to get used to Linux (and importantly, the terminal), I realize that there are some things that can only be done with the terminal. These are situations where I would otherwise have to use an online tool, do some tedious manual work, or it's just something that's only really possible with the terminal.

I will update this as I remember more tips. I've been using Linux for long enough that I won't be able to think of everything Linux-specific I do, it's just normal to me now.

Some of these will be things that I would only have to do once per installation, so I can remember how I did them in the future.

If you don't use Linux, you can still look through this to see what kinds of things you can do with the terminal.


Some Basics

Open a terminal on most Linux distros with Ctrl+Alt+T, or find it in whatever equivalent of a start menu you have.

The terminal always has a directory that it runs commands in context to. It will show you what folder you're in to the left of where you input your command.

kiophen@gelputer:~$ _

The Directory Structure

When you start a terminal, that folder (or 'current working directory') will be your home folder, which is at /home/YourUsername. It's commonly represented by a tilde (~) as a shorthand, which you can also use in the same way when typing commands.

The home folder in Linux is like the user folder on Windows. It comes with folders for documents, pictures, music, etc. It also has hidden folders for some user-specific configurations, much like the appdata folder in Windows.

On Windows, the highest directory of the drive Windows is installed on is C:/ but on Linux it's just simply / (a slash).

Keyboard Shortcuts In The Terminal

If you are new to this and have tried to copy and paste text in the terminal, you might have noticed it fucks up and doesn't do what it's supposed to. Here is what you actually want:

SUDO !!!!

Depending on what command you're running or what files you're running a command on, you might need elevated (or "root") permission. For example, installing a package or editing a file in /etc can give you a "permission denied" message. The way you do this is by preceding the command with "sudo".

sudo mkdir /etc/example

(This command creates a folder ("MaKes a DIRectory") named example in /etc.)

You will be prompted for your password, and then it will execute whatever command you typed after sudo with the highest permission possible.

Without sudo, you are fairly safe to mess around running commands. You are still able to erase your own files in your home folder with rm and rmdir though, which could be bad if you have data you don't want to lose.

BUT!!!!! WATCH OUT!!!

If you use sudo with the wrong command, you can completely fuck up your installation beyond reason and need to start from scratch. Be very careful with uninstalling packages and deleting files, because with sudo your computer WILL trust that you know what you're doing when you say delete system32.

But if you're just pasting commands from a tutorial, you're probably fine. I've never seen malicious commands online personally. It's still good to know at least what commands it's telling you to run though.

(Your Linux distro will almost 100% have safeguards in place to keep you from deleting your root directory, but still. Don't do that)


Downloading Youtube/Twitter Videos

You know when you want to download a song from a youtube video and then you spend a couple minutes trying whatever "youtube to mp3" result shows up on google? and then the first one doesn't work so you have to try the second result? and then that one takes an absurd amount of time to work? and then the next one asks you to type in your email address and they say they'll email you the mp3 in 1-2 business days

To permanently avoid this, I use a package called yt-dlp. You can find it in your package manager or using apt-get.

It has a terrifying amount of options, but is also simple if you want it to be. To download a video, you just open your terminal in the folder you want to download the video to and type the following:

yt-dlp "VideoUrlGoesHere"

It will download the best quality video file available directly to your computer.

If you just want the mp3, then just add this:

yt-dlp "VideoUrlGoesHere" -x --audio-format mp3

"-x" is the flag to eXtract the audio, and "--audio-format mp3" forces the audio to be mp3. Otherwise you could get some weird file formats.

*IMPORTANT! You can replace the video url with a *youtube playlist url* and it will download everything in the playlist. You can also replace it with a tweet URL to download the video in the tweet. It probably works with other websites as well.

Insane Specificity

There are a lot of other flags you can add to make yt-dlp download videos in the most specific ways beyond your comprehension:

yt-dlp "UrlGoesHere" -f "bestaudio+bestvideo[width<=360]" -w --max-filesize "15M" --remux-video "mp4"

Here's the command I use to download the videos for my gelplayer on my homepage here. It will download a video with the best audio available, with a video-quality of only 360p or lower. It will also refuse to download if the file ends up being over 15mb, and then makes sure it's an mp4 in the end. The -w makes it not overwrite any existing files.


List Files In A Folder

ls foldername > dir.txt

This will make a file called dir.txt that has all the filenames of files in the chosen folder, one filename per line.

I used this for making lists of files for my gelmusic player and gelsoft gallery page. I'm not sure why else you would want to do this other than for programming projects.


Find Files That Contain Specific Text..With Grep!

grep -ir "arbitrary string"

This returns a list of every file that contains the text "arbitrary string" in your current directory and any of its subfolders.

The i in -ir is a flag that makes the search case-insensitive, so it'll find files with "Arbitrary String" or "ARBITRARY STRING" as well.

The r in -ir is for "recursion"; without it this command would only search the current directory without checking any subfolders.

It's useful if you're editing a lot of text files, or trying to find a certain configuration file, or just any text file you're trying to find where you remember some contents but not the filename.

Regex (for grep!)

grep -ir "the.*dog"

You can also use regular expressions with grep. Regex is not very complicated if you just want to do something simple, but it is very powerful and has a LOT of options. Grep is just one of millions of programs that use regex

The above example command would return files containing any text that matches this pattern: the[any number of any characters]dog

A period (.) in regex matches any character, and a star (*) means "the previous character, any number of times (including zero)".

For example, it would match files containing "The dog", "the big dog", "the 410,757,864,530 dogs", "then a dog pooped", etc.

However, grep only looks at 1 line at a time. So it would NOT match a file that has "the" in the first line, and then "dog" on a later line.

It will also match things that might not be expected, like "The fox went outside. His dog friend ate grass", or "aetheric endogenicism". If you've ever heard someone complaining about regex, it's because of the pain of having to craft an extremely specific expression to avoid accidental matches that looks like a incomprehensible fucking nightmare as soon as you stop looking at it for a couple seconds.

You don't need to worry about that unless you're coding, though. If you're just manually trying to find a file with your own eyeballs you will be able to understand on your own if a file is talking about dogs or not.

Grepping Other Commands

Grep can be used on the outputs of commands as well, which makes it very useful for commands that spit out a ton of text that you only need to read a little bit of.


Routing Audio

There are a few applications for this, but my case is just this: I want OBS to only record certain program's audio, while ignoring all other programs. I want to be able to stream games while in a call and not broadcast me & my friend's voices.

On Windows, I think OBS has built-in support to just grab a program's audio now. It used to not be the case, and you'd have to use a program like Virtual Audio Cables to do this. On Linux, this is not supported yet, so I have to run some commands in the terminal.

pacmd load-module module-null-sink sink_name=game_sink sink_properties=device.description=Game-Sink

The above command makes a new "audio sink" called Game-Sink with pulseaudio. I am not going to explain pulseaudio, no one knows how it works. I found this command on the internet.

Game-Sink will be our virtual audio device that OBS will be able to record audio from. I can switch a program's audio device using my volume control settings. However, the audio won't be outputting through my headphones, since that's a different device. I can solve this by making my headphones the monitor device for Game-Sink.

pacmd list-sinks | grep name:

This command lists out the audio sinks. The output that I get is below, yours will be different.

name: <alsa_output.pci-0000_0e_00.1.hdmi-stereo-extra2>

name: <alsa_output.pci-0000_10_00.4.analog-stereo>

name: <game_sink>

I see my HDMI screen's speaker and an analog-stereo sink, along with the Game-Sink I just made. My headphones are the analog-stereo, so I'll take that whole string of text inside the arrow brackets and use it in the next command:

pacmd load-module module-loopback source="game_sink.monitor" sink="HEADPHONES SINK NAME HERE"

Now I can set a program's audio device to Game-Sink through my system volume control, and then make an audio device capture in OBS that captures Game-Sink.

This will only last until you turn your computer off. To make it permanent, I have to make a file in /etc/pulse/default.pa.d/ (create the default.pa.d folder if it doesn't exist) called anything.pa and paste the above commands, excluding the "pacmd". You will need sudo permissions to mess with files in /etc/.