linux commands that read specific parts of files - linux

I'm having some troubles with homework questions for an intro linux class I am taking. Two questions in particular are really throwing me for a loop. I don't know what commands to use to list off specific parts of specific lines all in one command. This is the first time I've used linux and none of it was covered in lecture. Here are a few of the questions. I don't want the answers, just somewhere to start with the commands.
(1 command : ½ pt) You should be in your home directory.
Create a file named summer-school in your home directory that contains the file names and lines (the data, not the line numbers) in those files where students scored less than ten out of a hundred.
The files look like this:
mary;:;79/100;st407
mary;:;51/100;m395
mary;:;71/100;cs541
mary;:;75/100;ph535
mary;:;77/100;ws420
mary;:;97/100;c392
mary;:;79/100;st638
mary;:;82/100;ar523
(2 commands : 1 pt) You should be in the ~cs155/pub/hw2Files/ directory.
(Do not add this change of directory command to your list of commands)
Create a file named classes in your home directory with a list of all the classes taken by anyone.
Uses the same type of files.
Thanks for the help

Related

How to get the last added folder to a directory [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 1 year ago.
Improve this question
I work in a software development company and every day upon my machine boot I have to execute the same commands to start coding. I recently decided to create a bash script to do that for me. The problem is that the commands I need to type have a single difference from one another, and that is the folder I need to access.
I always have to access a directory where it will contain folders with different versions of the company code (let's call it "codes" for the sake of the discussion), and everyday another folder is added to the "codes" directory (they update the company code everyday) with name as timestamp e.g. 2021-07-05-17-52-51.
To be able to create my automation script I need to be able to get into the "codes" directory and get the most recent folder added to it, or get the latest timestamp.
I am new to bash and I couldn't find answers on how to get the last added folder to a directory using bash or someway to use tab and get the last one.
You can use something like this:
directory=$(ls -At1 | head -n 1)
An explanation in parts:
ls -At1 lists sorted by time with one entry per line
head -n 1 returns the first entry
$(...) runs the command as a subshell, evaluates, and sets directory to the name of the item with the most recent modified datestamp. If you want to ignore hidden files and folders, you can lose the -A flag from ls.

How to search for files faster in Sublime Text 3

Right now I do ⌘t then scroll through autocomplete, or start typing the name (but half the time it doesn't find it).
Sublime doesn't find a file in many cases. For example, I typically have all my files called index.<ext> nested inside some folder. So I might have:
my/long/directory/structure/index.js
my/long/directory/structure2/index.js
my/long/directory/structure3/index.js
my/long/directory/structure.../index.js
my/long/directory/structuren/index.js
my/long/directory/index.js
my/long/directory2/index.js
my/long/directory.../index.js
my/long/directoryn/index.js
my/long/index.js
my/index.js
...
But in sublime you have to search for an exact path. I can't search this:
my directory index
And get results for directory, directory2, directory..., directoryn, I just get empty results because there is not my/directory. I can't remember the full folder path most of the time, so it takes a lot of effort to do so and I end up just navigating in the sidebar to find the file which takes some time.
Wondering if there is a better/faster way of doing this. Basically searching for a file by snippets/keywords of the complete path. So m dir would return my/long/directory, etc.
The first thing to note is that you do not have to search for an exact path; anywhere that Sublime provides you a list of items to select from and a text entry, fuzzy matching is in play. In your example searching just for idx will narrow down the list to all items that have those characters in that order, even if they're not adjacent to each other.
The entries show you visually how they're matching up, and there's a fairly sophisticated system behind the scenes that decides which characters make the best matches (relative to some hidden scoring algorithm):
In addition to this you can use multiple space separated terms to filter down the list. Each term is applied to the list of items resulting from the prior term, so they don't need to be provided in the same order as they appear in the file names.
This helps with searches where you know generally the name of the file, and from there can further drill down on segments of the path or other terms that will help narrow things down:
Something to note here is that as seen in these images, the folder structure is my/long/directory/structure, but the names of the files as seen in the panel don't include the my/ at the start.
In cases where your project contains only one top level folder, that folder isn't presented in the names of the files. Presumably this is because it's common to every file and thus not going to be a useful filter. As such trying to use my in the search field will return no matches unless one of the files has an m and a y somewhere in their filenames.
This isn't the case if there are multiple top level folders; in that case Sublime will include the root folder in the names of the files presented because now it's required to be able to distinguish between files in the different folders:
In addition to this, note that for any given filter text you enter in a panel, Sublime remembers the full text of the item that you selected while that filter text was being used, and uses that in it's scoring to prioritize the matches the next time you search in the same panel. The next time you search with the same term, Sublime will automatically pre-select the item that you picked last time under the theory that you probably want it again.
The search terms and their matches are saved in the session file and in your project's sublime-workspace files, so as you move from window to window and project to project you're essentially training Sublime how to find the files that you want.
My advice would be to try and flip your thinking a little bit. In my opinion the power of the fuzzy matching algorithm works best when you try to find files in a more organic way than trying to replicate the path entirely.
Instead, I would throw a few characters from the name of the file that I'm trying to find first, and then add another term that filters on some part of the path that will disambiguate things more; a term of idx s1 in this example immediately finds the two index.js files that are contained in structure1 folders, for example.
In a more real world example the names of the folders might contain the names of the components that they're a part of or something else that is providing a logical structure to the code, so you might do idx con to pull the index.js from the controller folder or idx mod to find the one in the model folder, and so on.
Regarding a better/faster way to do this I don't think there is one, at least in the general case. Sublime inherently knows every file that's in your project as a part of indexing all of the files to power other features such as Goto Symbol and it uses file watchers to detect changes to the structure of the open folders.
Anything else, including a third party plugin or package, would need to first do a redundant file scan to accumulate the list of files and would also have to replicate the file watching that Sublime is already doing in order to know when things change.

Linux terminal script to create boilerplate files in current working directory with one varying word?

I have to create two boilerplate files, both of which always have the same content, with the EXCEPTION of a single word. I'm thinking of creating a command or something that I can run in the Linux terminal (Ubuntu), along with an argument that represents the one word which can vary in the files created. Perhaps a batch file will accomplish this, but I don't know what it will look like.
I will be able to run this command every time I create these boilerplate files, instead of pasting the boilerplate and changing the one word in the file that has to be changed.
These file paths relative to my current working directory are:
registration.php
etc/module.xml
A simple Python script that reads in the file as string and replaces the occurrence would probably be the quickest. Something like:
with open('somefile.txt', 'r+') as inputFile:
txt=inputFile.read().replace('someword', 'replacementword')
inputFile.seek(0)
inputFile.write(txt)
inputfile.close()

ubuntu and unix stuff about directories and hidden files

In no way do i think i'm an adequate unix administrator but i'm learning. I "cd" into a specific directory and it appears to be empty after i do a "ls". But when i "ll" it says this:
/integration/import$ ll
total 184
What is this total 184? And how do i see these text files. I've never seen anything like this before. super confusing.
My co-worker had originally said this: in the imports folder find the text file containing this order and move it out of the folder/queue.
The below should list the hidden files as well. Usually, hidden files are the one starting with a dot. e.g. .mail or so.
ls -latr /integration/import/

Matlab search path for all users on linux

How can I add a Matlab search path for all users on a Linux system?
I am managing a Linux computer that is shared by several people.
I want to place some Matlab *.m files at a path (for example, /usr/local/matlab/our_matlab_scripts/) which everyone has a read access permission.
I also want to add this path to the Matlab search paths of all users
so that they can start using the Matlab files immediately.
As a single user, I learned that I can add a search path by Matlab 'addpath' command or from the file menu of Matlab, for example.
However, so far I could not find a way to do it as an administrator for all the current and future users at once.
I would be grateful if you could kindly teach me.
If you look at this thread:
http://www.mathworks.com/matlabcentral/newsreader/view_thread/112560
you will find that there is a file called matlabrc.m that is executed at startup. It looks for the startup.m file that users can have. One could put the addpath commands in this file and it should work for all users.

Resources